##// END OF EJS Templates
filter out some python 2.6 deprecationwarnings
Ville M. Vainio -
Show More
@@ -1,970 +1,973 b''
1 """ path.py - An object representing a path to a file or directory.
1 """ path.py - An object representing a path to a file or directory.
2
2
3 Example:
3 Example:
4
4
5 from IPython.external.path import path
5 from IPython.external.path import path
6 d = path('/home/guido/bin')
6 d = path('/home/guido/bin')
7 for f in d.files('*.py'):
7 for f in d.files('*.py'):
8 f.chmod(0755)
8 f.chmod(0755)
9
9
10 This module requires Python 2.2 or later.
10 This module requires Python 2.2 or later.
11
11
12
12
13 URL: http://www.jorendorff.com/articles/python/path
13 URL: http://www.jorendorff.com/articles/python/path
14 Author: Jason Orendorff <jason.orendorff\x40gmail\x2ecom> (and others - see the url!)
14 Author: Jason Orendorff <jason.orendorff\x40gmail\x2ecom> (and others - see the url!)
15 Date: 9 Mar 2007
15 Date: 9 Mar 2007
16 """
16 """
17
17
18
18
19 # TODO
19 # TODO
20 # - Tree-walking functions don't avoid symlink loops. Matt Harrison
20 # - Tree-walking functions don't avoid symlink loops. Matt Harrison
21 # sent me a patch for this.
21 # sent me a patch for this.
22 # - Bug in write_text(). It doesn't support Universal newline mode.
22 # - Bug in write_text(). It doesn't support Universal newline mode.
23 # - Better error message in listdir() when self isn't a
23 # - Better error message in listdir() when self isn't a
24 # directory. (On Windows, the error message really sucks.)
24 # directory. (On Windows, the error message really sucks.)
25 # - Make sure everything has a good docstring.
25 # - Make sure everything has a good docstring.
26 # - Add methods for regex find and replace.
26 # - Add methods for regex find and replace.
27 # - guess_content_type() method?
27 # - guess_content_type() method?
28 # - Perhaps support arguments to touch().
28 # - Perhaps support arguments to touch().
29
29
30 from __future__ import generators
30 from __future__ import generators
31
31
32 import sys, warnings, os, fnmatch, glob, shutil, codecs, md5
32 import sys, warnings, os, fnmatch, glob, shutil, codecs
33 # deprecated in python 2.6
34 warnings.filterwarnings('ignore', r'.*md5.*')
35 import md5
33
36
34 __version__ = '2.2'
37 __version__ = '2.2'
35 __all__ = ['path']
38 __all__ = ['path']
36
39
37 # Platform-specific support for path.owner
40 # Platform-specific support for path.owner
38 if os.name == 'nt':
41 if os.name == 'nt':
39 try:
42 try:
40 import win32security
43 import win32security
41 except ImportError:
44 except ImportError:
42 win32security = None
45 win32security = None
43 else:
46 else:
44 try:
47 try:
45 import pwd
48 import pwd
46 except ImportError:
49 except ImportError:
47 pwd = None
50 pwd = None
48
51
49 # Pre-2.3 support. Are unicode filenames supported?
52 # Pre-2.3 support. Are unicode filenames supported?
50 _base = str
53 _base = str
51 _getcwd = os.getcwd
54 _getcwd = os.getcwd
52 try:
55 try:
53 if os.path.supports_unicode_filenames:
56 if os.path.supports_unicode_filenames:
54 _base = unicode
57 _base = unicode
55 _getcwd = os.getcwdu
58 _getcwd = os.getcwdu
56 except AttributeError:
59 except AttributeError:
57 pass
60 pass
58
61
59 # Pre-2.3 workaround for booleans
62 # Pre-2.3 workaround for booleans
60 try:
63 try:
61 True, False
64 True, False
62 except NameError:
65 except NameError:
63 True, False = 1, 0
66 True, False = 1, 0
64
67
65 # Pre-2.3 workaround for basestring.
68 # Pre-2.3 workaround for basestring.
66 try:
69 try:
67 basestring
70 basestring
68 except NameError:
71 except NameError:
69 basestring = (str, unicode)
72 basestring = (str, unicode)
70
73
71 # Universal newline support
74 # Universal newline support
72 _textmode = 'r'
75 _textmode = 'r'
73 if hasattr(file, 'newlines'):
76 if hasattr(file, 'newlines'):
74 _textmode = 'U'
77 _textmode = 'U'
75
78
76
79
77 class TreeWalkWarning(Warning):
80 class TreeWalkWarning(Warning):
78 pass
81 pass
79
82
80 class path(_base):
83 class path(_base):
81 """ Represents a filesystem path.
84 """ Represents a filesystem path.
82
85
83 For documentation on individual methods, consult their
86 For documentation on individual methods, consult their
84 counterparts in os.path.
87 counterparts in os.path.
85 """
88 """
86
89
87 # --- Special Python methods.
90 # --- Special Python methods.
88
91
89 def __repr__(self):
92 def __repr__(self):
90 return 'path(%s)' % _base.__repr__(self)
93 return 'path(%s)' % _base.__repr__(self)
91
94
92 # Adding a path and a string yields a path.
95 # Adding a path and a string yields a path.
93 def __add__(self, more):
96 def __add__(self, more):
94 try:
97 try:
95 resultStr = _base.__add__(self, more)
98 resultStr = _base.__add__(self, more)
96 except TypeError: #Python bug
99 except TypeError: #Python bug
97 resultStr = NotImplemented
100 resultStr = NotImplemented
98 if resultStr is NotImplemented:
101 if resultStr is NotImplemented:
99 return resultStr
102 return resultStr
100 return self.__class__(resultStr)
103 return self.__class__(resultStr)
101
104
102 def __radd__(self, other):
105 def __radd__(self, other):
103 if isinstance(other, basestring):
106 if isinstance(other, basestring):
104 return self.__class__(other.__add__(self))
107 return self.__class__(other.__add__(self))
105 else:
108 else:
106 return NotImplemented
109 return NotImplemented
107
110
108 # The / operator joins paths.
111 # The / operator joins paths.
109 def __div__(self, rel):
112 def __div__(self, rel):
110 """ fp.__div__(rel) == fp / rel == fp.joinpath(rel)
113 """ fp.__div__(rel) == fp / rel == fp.joinpath(rel)
111
114
112 Join two path components, adding a separator character if
115 Join two path components, adding a separator character if
113 needed.
116 needed.
114 """
117 """
115 return self.__class__(os.path.join(self, rel))
118 return self.__class__(os.path.join(self, rel))
116
119
117 # Make the / operator work even when true division is enabled.
120 # Make the / operator work even when true division is enabled.
118 __truediv__ = __div__
121 __truediv__ = __div__
119
122
120 def getcwd(cls):
123 def getcwd(cls):
121 """ Return the current working directory as a path object. """
124 """ Return the current working directory as a path object. """
122 return cls(_getcwd())
125 return cls(_getcwd())
123 getcwd = classmethod(getcwd)
126 getcwd = classmethod(getcwd)
124
127
125
128
126 # --- Operations on path strings.
129 # --- Operations on path strings.
127
130
128 isabs = os.path.isabs
131 isabs = os.path.isabs
129 def abspath(self): return self.__class__(os.path.abspath(self))
132 def abspath(self): return self.__class__(os.path.abspath(self))
130 def normcase(self): return self.__class__(os.path.normcase(self))
133 def normcase(self): return self.__class__(os.path.normcase(self))
131 def normpath(self): return self.__class__(os.path.normpath(self))
134 def normpath(self): return self.__class__(os.path.normpath(self))
132 def realpath(self): return self.__class__(os.path.realpath(self))
135 def realpath(self): return self.__class__(os.path.realpath(self))
133 def expanduser(self): return self.__class__(os.path.expanduser(self))
136 def expanduser(self): return self.__class__(os.path.expanduser(self))
134 def expandvars(self): return self.__class__(os.path.expandvars(self))
137 def expandvars(self): return self.__class__(os.path.expandvars(self))
135 def dirname(self): return self.__class__(os.path.dirname(self))
138 def dirname(self): return self.__class__(os.path.dirname(self))
136 basename = os.path.basename
139 basename = os.path.basename
137
140
138 def expand(self):
141 def expand(self):
139 """ Clean up a filename by calling expandvars(),
142 """ Clean up a filename by calling expandvars(),
140 expanduser(), and normpath() on it.
143 expanduser(), and normpath() on it.
141
144
142 This is commonly everything needed to clean up a filename
145 This is commonly everything needed to clean up a filename
143 read from a configuration file, for example.
146 read from a configuration file, for example.
144 """
147 """
145 return self.expandvars().expanduser().normpath()
148 return self.expandvars().expanduser().normpath()
146
149
147 def _get_namebase(self):
150 def _get_namebase(self):
148 base, ext = os.path.splitext(self.name)
151 base, ext = os.path.splitext(self.name)
149 return base
152 return base
150
153
151 def _get_ext(self):
154 def _get_ext(self):
152 f, ext = os.path.splitext(_base(self))
155 f, ext = os.path.splitext(_base(self))
153 return ext
156 return ext
154
157
155 def _get_drive(self):
158 def _get_drive(self):
156 drive, r = os.path.splitdrive(self)
159 drive, r = os.path.splitdrive(self)
157 return self.__class__(drive)
160 return self.__class__(drive)
158
161
159 parent = property(
162 parent = property(
160 dirname, None, None,
163 dirname, None, None,
161 """ This path's parent directory, as a new path object.
164 """ This path's parent directory, as a new path object.
162
165
163 For example, path('/usr/local/lib/libpython.so').parent == path('/usr/local/lib')
166 For example, path('/usr/local/lib/libpython.so').parent == path('/usr/local/lib')
164 """)
167 """)
165
168
166 name = property(
169 name = property(
167 basename, None, None,
170 basename, None, None,
168 """ The name of this file or directory without the full path.
171 """ The name of this file or directory without the full path.
169
172
170 For example, path('/usr/local/lib/libpython.so').name == 'libpython.so'
173 For example, path('/usr/local/lib/libpython.so').name == 'libpython.so'
171 """)
174 """)
172
175
173 namebase = property(
176 namebase = property(
174 _get_namebase, None, None,
177 _get_namebase, None, None,
175 """ The same as path.name, but with one file extension stripped off.
178 """ The same as path.name, but with one file extension stripped off.
176
179
177 For example, path('/home/guido/python.tar.gz').name == 'python.tar.gz',
180 For example, path('/home/guido/python.tar.gz').name == 'python.tar.gz',
178 but path('/home/guido/python.tar.gz').namebase == 'python.tar'
181 but path('/home/guido/python.tar.gz').namebase == 'python.tar'
179 """)
182 """)
180
183
181 ext = property(
184 ext = property(
182 _get_ext, None, None,
185 _get_ext, None, None,
183 """ The file extension, for example '.py'. """)
186 """ The file extension, for example '.py'. """)
184
187
185 drive = property(
188 drive = property(
186 _get_drive, None, None,
189 _get_drive, None, None,
187 """ The drive specifier, for example 'C:'.
190 """ The drive specifier, for example 'C:'.
188 This is always empty on systems that don't use drive specifiers.
191 This is always empty on systems that don't use drive specifiers.
189 """)
192 """)
190
193
191 def splitpath(self):
194 def splitpath(self):
192 """ p.splitpath() -> Return (p.parent, p.name). """
195 """ p.splitpath() -> Return (p.parent, p.name). """
193 parent, child = os.path.split(self)
196 parent, child = os.path.split(self)
194 return self.__class__(parent), child
197 return self.__class__(parent), child
195
198
196 def splitdrive(self):
199 def splitdrive(self):
197 """ p.splitdrive() -> Return (p.drive, <the rest of p>).
200 """ p.splitdrive() -> Return (p.drive, <the rest of p>).
198
201
199 Split the drive specifier from this path. If there is
202 Split the drive specifier from this path. If there is
200 no drive specifier, p.drive is empty, so the return value
203 no drive specifier, p.drive is empty, so the return value
201 is simply (path(''), p). This is always the case on Unix.
204 is simply (path(''), p). This is always the case on Unix.
202 """
205 """
203 drive, rel = os.path.splitdrive(self)
206 drive, rel = os.path.splitdrive(self)
204 return self.__class__(drive), rel
207 return self.__class__(drive), rel
205
208
206 def splitext(self):
209 def splitext(self):
207 """ p.splitext() -> Return (p.stripext(), p.ext).
210 """ p.splitext() -> Return (p.stripext(), p.ext).
208
211
209 Split the filename extension from this path and return
212 Split the filename extension from this path and return
210 the two parts. Either part may be empty.
213 the two parts. Either part may be empty.
211
214
212 The extension is everything from '.' to the end of the
215 The extension is everything from '.' to the end of the
213 last path segment. This has the property that if
216 last path segment. This has the property that if
214 (a, b) == p.splitext(), then a + b == p.
217 (a, b) == p.splitext(), then a + b == p.
215 """
218 """
216 filename, ext = os.path.splitext(self)
219 filename, ext = os.path.splitext(self)
217 return self.__class__(filename), ext
220 return self.__class__(filename), ext
218
221
219 def stripext(self):
222 def stripext(self):
220 """ p.stripext() -> Remove one file extension from the path.
223 """ p.stripext() -> Remove one file extension from the path.
221
224
222 For example, path('/home/guido/python.tar.gz').stripext()
225 For example, path('/home/guido/python.tar.gz').stripext()
223 returns path('/home/guido/python.tar').
226 returns path('/home/guido/python.tar').
224 """
227 """
225 return self.splitext()[0]
228 return self.splitext()[0]
226
229
227 if hasattr(os.path, 'splitunc'):
230 if hasattr(os.path, 'splitunc'):
228 def splitunc(self):
231 def splitunc(self):
229 unc, rest = os.path.splitunc(self)
232 unc, rest = os.path.splitunc(self)
230 return self.__class__(unc), rest
233 return self.__class__(unc), rest
231
234
232 def _get_uncshare(self):
235 def _get_uncshare(self):
233 unc, r = os.path.splitunc(self)
236 unc, r = os.path.splitunc(self)
234 return self.__class__(unc)
237 return self.__class__(unc)
235
238
236 uncshare = property(
239 uncshare = property(
237 _get_uncshare, None, None,
240 _get_uncshare, None, None,
238 """ The UNC mount point for this path.
241 """ The UNC mount point for this path.
239 This is empty for paths on local drives. """)
242 This is empty for paths on local drives. """)
240
243
241 def joinpath(self, *args):
244 def joinpath(self, *args):
242 """ Join two or more path components, adding a separator
245 """ Join two or more path components, adding a separator
243 character (os.sep) if needed. Returns a new path
246 character (os.sep) if needed. Returns a new path
244 object.
247 object.
245 """
248 """
246 return self.__class__(os.path.join(self, *args))
249 return self.__class__(os.path.join(self, *args))
247
250
248 def splitall(self):
251 def splitall(self):
249 r""" Return a list of the path components in this path.
252 r""" Return a list of the path components in this path.
250
253
251 The first item in the list will be a path. Its value will be
254 The first item in the list will be a path. Its value will be
252 either os.curdir, os.pardir, empty, or the root directory of
255 either os.curdir, os.pardir, empty, or the root directory of
253 this path (for example, '/' or 'C:\\'). The other items in
256 this path (for example, '/' or 'C:\\'). The other items in
254 the list will be strings.
257 the list will be strings.
255
258
256 path.path.joinpath(*result) will yield the original path.
259 path.path.joinpath(*result) will yield the original path.
257 """
260 """
258 parts = []
261 parts = []
259 loc = self
262 loc = self
260 while loc != os.curdir and loc != os.pardir:
263 while loc != os.curdir and loc != os.pardir:
261 prev = loc
264 prev = loc
262 loc, child = prev.splitpath()
265 loc, child = prev.splitpath()
263 if loc == prev:
266 if loc == prev:
264 break
267 break
265 parts.append(child)
268 parts.append(child)
266 parts.append(loc)
269 parts.append(loc)
267 parts.reverse()
270 parts.reverse()
268 return parts
271 return parts
269
272
270 def relpath(self):
273 def relpath(self):
271 """ Return this path as a relative path,
274 """ Return this path as a relative path,
272 based from the current working directory.
275 based from the current working directory.
273 """
276 """
274 cwd = self.__class__(os.getcwd())
277 cwd = self.__class__(os.getcwd())
275 return cwd.relpathto(self)
278 return cwd.relpathto(self)
276
279
277 def relpathto(self, dest):
280 def relpathto(self, dest):
278 """ Return a relative path from self to dest.
281 """ Return a relative path from self to dest.
279
282
280 If there is no relative path from self to dest, for example if
283 If there is no relative path from self to dest, for example if
281 they reside on different drives in Windows, then this returns
284 they reside on different drives in Windows, then this returns
282 dest.abspath().
285 dest.abspath().
283 """
286 """
284 origin = self.abspath()
287 origin = self.abspath()
285 dest = self.__class__(dest).abspath()
288 dest = self.__class__(dest).abspath()
286
289
287 orig_list = origin.normcase().splitall()
290 orig_list = origin.normcase().splitall()
288 # Don't normcase dest! We want to preserve the case.
291 # Don't normcase dest! We want to preserve the case.
289 dest_list = dest.splitall()
292 dest_list = dest.splitall()
290
293
291 if orig_list[0] != os.path.normcase(dest_list[0]):
294 if orig_list[0] != os.path.normcase(dest_list[0]):
292 # Can't get here from there.
295 # Can't get here from there.
293 return dest
296 return dest
294
297
295 # Find the location where the two paths start to differ.
298 # Find the location where the two paths start to differ.
296 i = 0
299 i = 0
297 for start_seg, dest_seg in zip(orig_list, dest_list):
300 for start_seg, dest_seg in zip(orig_list, dest_list):
298 if start_seg != os.path.normcase(dest_seg):
301 if start_seg != os.path.normcase(dest_seg):
299 break
302 break
300 i += 1
303 i += 1
301
304
302 # Now i is the point where the two paths diverge.
305 # Now i is the point where the two paths diverge.
303 # Need a certain number of "os.pardir"s to work up
306 # Need a certain number of "os.pardir"s to work up
304 # from the origin to the point of divergence.
307 # from the origin to the point of divergence.
305 segments = [os.pardir] * (len(orig_list) - i)
308 segments = [os.pardir] * (len(orig_list) - i)
306 # Need to add the diverging part of dest_list.
309 # Need to add the diverging part of dest_list.
307 segments += dest_list[i:]
310 segments += dest_list[i:]
308 if len(segments) == 0:
311 if len(segments) == 0:
309 # If they happen to be identical, use os.curdir.
312 # If they happen to be identical, use os.curdir.
310 relpath = os.curdir
313 relpath = os.curdir
311 else:
314 else:
312 relpath = os.path.join(*segments)
315 relpath = os.path.join(*segments)
313 return self.__class__(relpath)
316 return self.__class__(relpath)
314
317
315 # --- Listing, searching, walking, and matching
318 # --- Listing, searching, walking, and matching
316
319
317 def listdir(self, pattern=None):
320 def listdir(self, pattern=None):
318 """ D.listdir() -> List of items in this directory.
321 """ D.listdir() -> List of items in this directory.
319
322
320 Use D.files() or D.dirs() instead if you want a listing
323 Use D.files() or D.dirs() instead if you want a listing
321 of just files or just subdirectories.
324 of just files or just subdirectories.
322
325
323 The elements of the list are path objects.
326 The elements of the list are path objects.
324
327
325 With the optional 'pattern' argument, this only lists
328 With the optional 'pattern' argument, this only lists
326 items whose names match the given pattern.
329 items whose names match the given pattern.
327 """
330 """
328 names = os.listdir(self)
331 names = os.listdir(self)
329 if pattern is not None:
332 if pattern is not None:
330 names = fnmatch.filter(names, pattern)
333 names = fnmatch.filter(names, pattern)
331 return [self / child for child in names]
334 return [self / child for child in names]
332
335
333 def dirs(self, pattern=None):
336 def dirs(self, pattern=None):
334 """ D.dirs() -> List of this directory's subdirectories.
337 """ D.dirs() -> List of this directory's subdirectories.
335
338
336 The elements of the list are path objects.
339 The elements of the list are path objects.
337 This does not walk recursively into subdirectories
340 This does not walk recursively into subdirectories
338 (but see path.walkdirs).
341 (but see path.walkdirs).
339
342
340 With the optional 'pattern' argument, this only lists
343 With the optional 'pattern' argument, this only lists
341 directories whose names match the given pattern. For
344 directories whose names match the given pattern. For
342 example, d.dirs('build-*').
345 example, d.dirs('build-*').
343 """
346 """
344 return [p for p in self.listdir(pattern) if p.isdir()]
347 return [p for p in self.listdir(pattern) if p.isdir()]
345
348
346 def files(self, pattern=None):
349 def files(self, pattern=None):
347 """ D.files() -> List of the files in this directory.
350 """ D.files() -> List of the files in this directory.
348
351
349 The elements of the list are path objects.
352 The elements of the list are path objects.
350 This does not walk into subdirectories (see path.walkfiles).
353 This does not walk into subdirectories (see path.walkfiles).
351
354
352 With the optional 'pattern' argument, this only lists files
355 With the optional 'pattern' argument, this only lists files
353 whose names match the given pattern. For example,
356 whose names match the given pattern. For example,
354 d.files('*.pyc').
357 d.files('*.pyc').
355 """
358 """
356
359
357 return [p for p in self.listdir(pattern) if p.isfile()]
360 return [p for p in self.listdir(pattern) if p.isfile()]
358
361
359 def walk(self, pattern=None, errors='strict'):
362 def walk(self, pattern=None, errors='strict'):
360 """ D.walk() -> iterator over files and subdirs, recursively.
363 """ D.walk() -> iterator over files and subdirs, recursively.
361
364
362 The iterator yields path objects naming each child item of
365 The iterator yields path objects naming each child item of
363 this directory and its descendants. This requires that
366 this directory and its descendants. This requires that
364 D.isdir().
367 D.isdir().
365
368
366 This performs a depth-first traversal of the directory tree.
369 This performs a depth-first traversal of the directory tree.
367 Each directory is returned just before all its children.
370 Each directory is returned just before all its children.
368
371
369 The errors= keyword argument controls behavior when an
372 The errors= keyword argument controls behavior when an
370 error occurs. The default is 'strict', which causes an
373 error occurs. The default is 'strict', which causes an
371 exception. The other allowed values are 'warn', which
374 exception. The other allowed values are 'warn', which
372 reports the error via warnings.warn(), and 'ignore'.
375 reports the error via warnings.warn(), and 'ignore'.
373 """
376 """
374 if errors not in ('strict', 'warn', 'ignore'):
377 if errors not in ('strict', 'warn', 'ignore'):
375 raise ValueError("invalid errors parameter")
378 raise ValueError("invalid errors parameter")
376
379
377 try:
380 try:
378 childList = self.listdir()
381 childList = self.listdir()
379 except Exception:
382 except Exception:
380 if errors == 'ignore':
383 if errors == 'ignore':
381 return
384 return
382 elif errors == 'warn':
385 elif errors == 'warn':
383 warnings.warn(
386 warnings.warn(
384 "Unable to list directory '%s': %s"
387 "Unable to list directory '%s': %s"
385 % (self, sys.exc_info()[1]),
388 % (self, sys.exc_info()[1]),
386 TreeWalkWarning)
389 TreeWalkWarning)
387 return
390 return
388 else:
391 else:
389 raise
392 raise
390
393
391 for child in childList:
394 for child in childList:
392 if pattern is None or child.fnmatch(pattern):
395 if pattern is None or child.fnmatch(pattern):
393 yield child
396 yield child
394 try:
397 try:
395 isdir = child.isdir()
398 isdir = child.isdir()
396 except Exception:
399 except Exception:
397 if errors == 'ignore':
400 if errors == 'ignore':
398 isdir = False
401 isdir = False
399 elif errors == 'warn':
402 elif errors == 'warn':
400 warnings.warn(
403 warnings.warn(
401 "Unable to access '%s': %s"
404 "Unable to access '%s': %s"
402 % (child, sys.exc_info()[1]),
405 % (child, sys.exc_info()[1]),
403 TreeWalkWarning)
406 TreeWalkWarning)
404 isdir = False
407 isdir = False
405 else:
408 else:
406 raise
409 raise
407
410
408 if isdir:
411 if isdir:
409 for item in child.walk(pattern, errors):
412 for item in child.walk(pattern, errors):
410 yield item
413 yield item
411
414
412 def walkdirs(self, pattern=None, errors='strict'):
415 def walkdirs(self, pattern=None, errors='strict'):
413 """ D.walkdirs() -> iterator over subdirs, recursively.
416 """ D.walkdirs() -> iterator over subdirs, recursively.
414
417
415 With the optional 'pattern' argument, this yields only
418 With the optional 'pattern' argument, this yields only
416 directories whose names match the given pattern. For
419 directories whose names match the given pattern. For
417 example, mydir.walkdirs('*test') yields only directories
420 example, mydir.walkdirs('*test') yields only directories
418 with names ending in 'test'.
421 with names ending in 'test'.
419
422
420 The errors= keyword argument controls behavior when an
423 The errors= keyword argument controls behavior when an
421 error occurs. The default is 'strict', which causes an
424 error occurs. The default is 'strict', which causes an
422 exception. The other allowed values are 'warn', which
425 exception. The other allowed values are 'warn', which
423 reports the error via warnings.warn(), and 'ignore'.
426 reports the error via warnings.warn(), and 'ignore'.
424 """
427 """
425 if errors not in ('strict', 'warn', 'ignore'):
428 if errors not in ('strict', 'warn', 'ignore'):
426 raise ValueError("invalid errors parameter")
429 raise ValueError("invalid errors parameter")
427
430
428 try:
431 try:
429 dirs = self.dirs()
432 dirs = self.dirs()
430 except Exception:
433 except Exception:
431 if errors == 'ignore':
434 if errors == 'ignore':
432 return
435 return
433 elif errors == 'warn':
436 elif errors == 'warn':
434 warnings.warn(
437 warnings.warn(
435 "Unable to list directory '%s': %s"
438 "Unable to list directory '%s': %s"
436 % (self, sys.exc_info()[1]),
439 % (self, sys.exc_info()[1]),
437 TreeWalkWarning)
440 TreeWalkWarning)
438 return
441 return
439 else:
442 else:
440 raise
443 raise
441
444
442 for child in dirs:
445 for child in dirs:
443 if pattern is None or child.fnmatch(pattern):
446 if pattern is None or child.fnmatch(pattern):
444 yield child
447 yield child
445 for subsubdir in child.walkdirs(pattern, errors):
448 for subsubdir in child.walkdirs(pattern, errors):
446 yield subsubdir
449 yield subsubdir
447
450
448 def walkfiles(self, pattern=None, errors='strict'):
451 def walkfiles(self, pattern=None, errors='strict'):
449 """ D.walkfiles() -> iterator over files in D, recursively.
452 """ D.walkfiles() -> iterator over files in D, recursively.
450
453
451 The optional argument, pattern, limits the results to files
454 The optional argument, pattern, limits the results to files
452 with names that match the pattern. For example,
455 with names that match the pattern. For example,
453 mydir.walkfiles('*.tmp') yields only files with the .tmp
456 mydir.walkfiles('*.tmp') yields only files with the .tmp
454 extension.
457 extension.
455 """
458 """
456 if errors not in ('strict', 'warn', 'ignore'):
459 if errors not in ('strict', 'warn', 'ignore'):
457 raise ValueError("invalid errors parameter")
460 raise ValueError("invalid errors parameter")
458
461
459 try:
462 try:
460 childList = self.listdir()
463 childList = self.listdir()
461 except Exception:
464 except Exception:
462 if errors == 'ignore':
465 if errors == 'ignore':
463 return
466 return
464 elif errors == 'warn':
467 elif errors == 'warn':
465 warnings.warn(
468 warnings.warn(
466 "Unable to list directory '%s': %s"
469 "Unable to list directory '%s': %s"
467 % (self, sys.exc_info()[1]),
470 % (self, sys.exc_info()[1]),
468 TreeWalkWarning)
471 TreeWalkWarning)
469 return
472 return
470 else:
473 else:
471 raise
474 raise
472
475
473 for child in childList:
476 for child in childList:
474 try:
477 try:
475 isfile = child.isfile()
478 isfile = child.isfile()
476 isdir = not isfile and child.isdir()
479 isdir = not isfile and child.isdir()
477 except:
480 except:
478 if errors == 'ignore':
481 if errors == 'ignore':
479 continue
482 continue
480 elif errors == 'warn':
483 elif errors == 'warn':
481 warnings.warn(
484 warnings.warn(
482 "Unable to access '%s': %s"
485 "Unable to access '%s': %s"
483 % (self, sys.exc_info()[1]),
486 % (self, sys.exc_info()[1]),
484 TreeWalkWarning)
487 TreeWalkWarning)
485 continue
488 continue
486 else:
489 else:
487 raise
490 raise
488
491
489 if isfile:
492 if isfile:
490 if pattern is None or child.fnmatch(pattern):
493 if pattern is None or child.fnmatch(pattern):
491 yield child
494 yield child
492 elif isdir:
495 elif isdir:
493 for f in child.walkfiles(pattern, errors):
496 for f in child.walkfiles(pattern, errors):
494 yield f
497 yield f
495
498
496 def fnmatch(self, pattern):
499 def fnmatch(self, pattern):
497 """ Return True if self.name matches the given pattern.
500 """ Return True if self.name matches the given pattern.
498
501
499 pattern - A filename pattern with wildcards,
502 pattern - A filename pattern with wildcards,
500 for example '*.py'.
503 for example '*.py'.
501 """
504 """
502 return fnmatch.fnmatch(self.name, pattern)
505 return fnmatch.fnmatch(self.name, pattern)
503
506
504 def glob(self, pattern):
507 def glob(self, pattern):
505 """ Return a list of path objects that match the pattern.
508 """ Return a list of path objects that match the pattern.
506
509
507 pattern - a path relative to this directory, with wildcards.
510 pattern - a path relative to this directory, with wildcards.
508
511
509 For example, path('/users').glob('*/bin/*') returns a list
512 For example, path('/users').glob('*/bin/*') returns a list
510 of all the files users have in their bin directories.
513 of all the files users have in their bin directories.
511 """
514 """
512 cls = self.__class__
515 cls = self.__class__
513 return [cls(s) for s in glob.glob(_base(self / pattern))]
516 return [cls(s) for s in glob.glob(_base(self / pattern))]
514
517
515
518
516 # --- Reading or writing an entire file at once.
519 # --- Reading or writing an entire file at once.
517
520
518 def open(self, mode='r'):
521 def open(self, mode='r'):
519 """ Open this file. Return a file object. """
522 """ Open this file. Return a file object. """
520 return file(self, mode)
523 return file(self, mode)
521
524
522 def bytes(self):
525 def bytes(self):
523 """ Open this file, read all bytes, return them as a string. """
526 """ Open this file, read all bytes, return them as a string. """
524 f = self.open('rb')
527 f = self.open('rb')
525 try:
528 try:
526 return f.read()
529 return f.read()
527 finally:
530 finally:
528 f.close()
531 f.close()
529
532
530 def write_bytes(self, bytes, append=False):
533 def write_bytes(self, bytes, append=False):
531 """ Open this file and write the given bytes to it.
534 """ Open this file and write the given bytes to it.
532
535
533 Default behavior is to overwrite any existing file.
536 Default behavior is to overwrite any existing file.
534 Call p.write_bytes(bytes, append=True) to append instead.
537 Call p.write_bytes(bytes, append=True) to append instead.
535 """
538 """
536 if append:
539 if append:
537 mode = 'ab'
540 mode = 'ab'
538 else:
541 else:
539 mode = 'wb'
542 mode = 'wb'
540 f = self.open(mode)
543 f = self.open(mode)
541 try:
544 try:
542 f.write(bytes)
545 f.write(bytes)
543 finally:
546 finally:
544 f.close()
547 f.close()
545
548
546 def text(self, encoding=None, errors='strict'):
549 def text(self, encoding=None, errors='strict'):
547 r""" Open this file, read it in, return the content as a string.
550 r""" Open this file, read it in, return the content as a string.
548
551
549 This uses 'U' mode in Python 2.3 and later, so '\r\n' and '\r'
552 This uses 'U' mode in Python 2.3 and later, so '\r\n' and '\r'
550 are automatically translated to '\n'.
553 are automatically translated to '\n'.
551
554
552 Optional arguments:
555 Optional arguments:
553
556
554 encoding - The Unicode encoding (or character set) of
557 encoding - The Unicode encoding (or character set) of
555 the file. If present, the content of the file is
558 the file. If present, the content of the file is
556 decoded and returned as a unicode object; otherwise
559 decoded and returned as a unicode object; otherwise
557 it is returned as an 8-bit str.
560 it is returned as an 8-bit str.
558 errors - How to handle Unicode errors; see help(str.decode)
561 errors - How to handle Unicode errors; see help(str.decode)
559 for the options. Default is 'strict'.
562 for the options. Default is 'strict'.
560 """
563 """
561 if encoding is None:
564 if encoding is None:
562 # 8-bit
565 # 8-bit
563 f = self.open(_textmode)
566 f = self.open(_textmode)
564 try:
567 try:
565 return f.read()
568 return f.read()
566 finally:
569 finally:
567 f.close()
570 f.close()
568 else:
571 else:
569 # Unicode
572 # Unicode
570 f = codecs.open(self, 'r', encoding, errors)
573 f = codecs.open(self, 'r', encoding, errors)
571 # (Note - Can't use 'U' mode here, since codecs.open
574 # (Note - Can't use 'U' mode here, since codecs.open
572 # doesn't support 'U' mode, even in Python 2.3.)
575 # doesn't support 'U' mode, even in Python 2.3.)
573 try:
576 try:
574 t = f.read()
577 t = f.read()
575 finally:
578 finally:
576 f.close()
579 f.close()
577 return (t.replace(u'\r\n', u'\n')
580 return (t.replace(u'\r\n', u'\n')
578 .replace(u'\r\x85', u'\n')
581 .replace(u'\r\x85', u'\n')
579 .replace(u'\r', u'\n')
582 .replace(u'\r', u'\n')
580 .replace(u'\x85', u'\n')
583 .replace(u'\x85', u'\n')
581 .replace(u'\u2028', u'\n'))
584 .replace(u'\u2028', u'\n'))
582
585
583 def write_text(self, text, encoding=None, errors='strict', linesep=os.linesep, append=False):
586 def write_text(self, text, encoding=None, errors='strict', linesep=os.linesep, append=False):
584 r""" Write the given text to this file.
587 r""" Write the given text to this file.
585
588
586 The default behavior is to overwrite any existing file;
589 The default behavior is to overwrite any existing file;
587 to append instead, use the 'append=True' keyword argument.
590 to append instead, use the 'append=True' keyword argument.
588
591
589 There are two differences between path.write_text() and
592 There are two differences between path.write_text() and
590 path.write_bytes(): newline handling and Unicode handling.
593 path.write_bytes(): newline handling and Unicode handling.
591 See below.
594 See below.
592
595
593 Parameters:
596 Parameters:
594
597
595 - text - str/unicode - The text to be written.
598 - text - str/unicode - The text to be written.
596
599
597 - encoding - str - The Unicode encoding that will be used.
600 - encoding - str - The Unicode encoding that will be used.
598 This is ignored if 'text' isn't a Unicode string.
601 This is ignored if 'text' isn't a Unicode string.
599
602
600 - errors - str - How to handle Unicode encoding errors.
603 - errors - str - How to handle Unicode encoding errors.
601 Default is 'strict'. See help(unicode.encode) for the
604 Default is 'strict'. See help(unicode.encode) for the
602 options. This is ignored if 'text' isn't a Unicode
605 options. This is ignored if 'text' isn't a Unicode
603 string.
606 string.
604
607
605 - linesep - keyword argument - str/unicode - The sequence of
608 - linesep - keyword argument - str/unicode - The sequence of
606 characters to be used to mark end-of-line. The default is
609 characters to be used to mark end-of-line. The default is
607 os.linesep. You can also specify None; this means to
610 os.linesep. You can also specify None; this means to
608 leave all newlines as they are in 'text'.
611 leave all newlines as they are in 'text'.
609
612
610 - append - keyword argument - bool - Specifies what to do if
613 - append - keyword argument - bool - Specifies what to do if
611 the file already exists (True: append to the end of it;
614 the file already exists (True: append to the end of it;
612 False: overwrite it.) The default is False.
615 False: overwrite it.) The default is False.
613
616
614
617
615 --- Newline handling.
618 --- Newline handling.
616
619
617 write_text() converts all standard end-of-line sequences
620 write_text() converts all standard end-of-line sequences
618 ('\n', '\r', and '\r\n') to your platform's default end-of-line
621 ('\n', '\r', and '\r\n') to your platform's default end-of-line
619 sequence (see os.linesep; on Windows, for example, the
622 sequence (see os.linesep; on Windows, for example, the
620 end-of-line marker is '\r\n').
623 end-of-line marker is '\r\n').
621
624
622 If you don't like your platform's default, you can override it
625 If you don't like your platform's default, you can override it
623 using the 'linesep=' keyword argument. If you specifically want
626 using the 'linesep=' keyword argument. If you specifically want
624 write_text() to preserve the newlines as-is, use 'linesep=None'.
627 write_text() to preserve the newlines as-is, use 'linesep=None'.
625
628
626 This applies to Unicode text the same as to 8-bit text, except
629 This applies to Unicode text the same as to 8-bit text, except
627 there are three additional standard Unicode end-of-line sequences:
630 there are three additional standard Unicode end-of-line sequences:
628 u'\x85', u'\r\x85', and u'\u2028'.
631 u'\x85', u'\r\x85', and u'\u2028'.
629
632
630 (This is slightly different from when you open a file for
633 (This is slightly different from when you open a file for
631 writing with fopen(filename, "w") in C or file(filename, 'w')
634 writing with fopen(filename, "w") in C or file(filename, 'w')
632 in Python.)
635 in Python.)
633
636
634
637
635 --- Unicode
638 --- Unicode
636
639
637 If 'text' isn't Unicode, then apart from newline handling, the
640 If 'text' isn't Unicode, then apart from newline handling, the
638 bytes are written verbatim to the file. The 'encoding' and
641 bytes are written verbatim to the file. The 'encoding' and
639 'errors' arguments are not used and must be omitted.
642 'errors' arguments are not used and must be omitted.
640
643
641 If 'text' is Unicode, it is first converted to bytes using the
644 If 'text' is Unicode, it is first converted to bytes using the
642 specified 'encoding' (or the default encoding if 'encoding'
645 specified 'encoding' (or the default encoding if 'encoding'
643 isn't specified). The 'errors' argument applies only to this
646 isn't specified). The 'errors' argument applies only to this
644 conversion.
647 conversion.
645
648
646 """
649 """
647 if isinstance(text, unicode):
650 if isinstance(text, unicode):
648 if linesep is not None:
651 if linesep is not None:
649 # Convert all standard end-of-line sequences to
652 # Convert all standard end-of-line sequences to
650 # ordinary newline characters.
653 # ordinary newline characters.
651 text = (text.replace(u'\r\n', u'\n')
654 text = (text.replace(u'\r\n', u'\n')
652 .replace(u'\r\x85', u'\n')
655 .replace(u'\r\x85', u'\n')
653 .replace(u'\r', u'\n')
656 .replace(u'\r', u'\n')
654 .replace(u'\x85', u'\n')
657 .replace(u'\x85', u'\n')
655 .replace(u'\u2028', u'\n'))
658 .replace(u'\u2028', u'\n'))
656 text = text.replace(u'\n', linesep)
659 text = text.replace(u'\n', linesep)
657 if encoding is None:
660 if encoding is None:
658 encoding = sys.getdefaultencoding()
661 encoding = sys.getdefaultencoding()
659 bytes = text.encode(encoding, errors)
662 bytes = text.encode(encoding, errors)
660 else:
663 else:
661 # It is an error to specify an encoding if 'text' is
664 # It is an error to specify an encoding if 'text' is
662 # an 8-bit string.
665 # an 8-bit string.
663 assert encoding is None
666 assert encoding is None
664
667
665 if linesep is not None:
668 if linesep is not None:
666 text = (text.replace('\r\n', '\n')
669 text = (text.replace('\r\n', '\n')
667 .replace('\r', '\n'))
670 .replace('\r', '\n'))
668 bytes = text.replace('\n', linesep)
671 bytes = text.replace('\n', linesep)
669
672
670 self.write_bytes(bytes, append)
673 self.write_bytes(bytes, append)
671
674
672 def lines(self, encoding=None, errors='strict', retain=True):
675 def lines(self, encoding=None, errors='strict', retain=True):
673 r""" Open this file, read all lines, return them in a list.
676 r""" Open this file, read all lines, return them in a list.
674
677
675 Optional arguments:
678 Optional arguments:
676 encoding - The Unicode encoding (or character set) of
679 encoding - The Unicode encoding (or character set) of
677 the file. The default is None, meaning the content
680 the file. The default is None, meaning the content
678 of the file is read as 8-bit characters and returned
681 of the file is read as 8-bit characters and returned
679 as a list of (non-Unicode) str objects.
682 as a list of (non-Unicode) str objects.
680 errors - How to handle Unicode errors; see help(str.decode)
683 errors - How to handle Unicode errors; see help(str.decode)
681 for the options. Default is 'strict'
684 for the options. Default is 'strict'
682 retain - If true, retain newline characters; but all newline
685 retain - If true, retain newline characters; but all newline
683 character combinations ('\r', '\n', '\r\n') are
686 character combinations ('\r', '\n', '\r\n') are
684 translated to '\n'. If false, newline characters are
687 translated to '\n'. If false, newline characters are
685 stripped off. Default is True.
688 stripped off. Default is True.
686
689
687 This uses 'U' mode in Python 2.3 and later.
690 This uses 'U' mode in Python 2.3 and later.
688 """
691 """
689 if encoding is None and retain:
692 if encoding is None and retain:
690 f = self.open(_textmode)
693 f = self.open(_textmode)
691 try:
694 try:
692 return f.readlines()
695 return f.readlines()
693 finally:
696 finally:
694 f.close()
697 f.close()
695 else:
698 else:
696 return self.text(encoding, errors).splitlines(retain)
699 return self.text(encoding, errors).splitlines(retain)
697
700
698 def write_lines(self, lines, encoding=None, errors='strict',
701 def write_lines(self, lines, encoding=None, errors='strict',
699 linesep=os.linesep, append=False):
702 linesep=os.linesep, append=False):
700 r""" Write the given lines of text to this file.
703 r""" Write the given lines of text to this file.
701
704
702 By default this overwrites any existing file at this path.
705 By default this overwrites any existing file at this path.
703
706
704 This puts a platform-specific newline sequence on every line.
707 This puts a platform-specific newline sequence on every line.
705 See 'linesep' below.
708 See 'linesep' below.
706
709
707 lines - A list of strings.
710 lines - A list of strings.
708
711
709 encoding - A Unicode encoding to use. This applies only if
712 encoding - A Unicode encoding to use. This applies only if
710 'lines' contains any Unicode strings.
713 'lines' contains any Unicode strings.
711
714
712 errors - How to handle errors in Unicode encoding. This
715 errors - How to handle errors in Unicode encoding. This
713 also applies only to Unicode strings.
716 also applies only to Unicode strings.
714
717
715 linesep - The desired line-ending. This line-ending is
718 linesep - The desired line-ending. This line-ending is
716 applied to every line. If a line already has any
719 applied to every line. If a line already has any
717 standard line ending ('\r', '\n', '\r\n', u'\x85',
720 standard line ending ('\r', '\n', '\r\n', u'\x85',
718 u'\r\x85', u'\u2028'), that will be stripped off and
721 u'\r\x85', u'\u2028'), that will be stripped off and
719 this will be used instead. The default is os.linesep,
722 this will be used instead. The default is os.linesep,
720 which is platform-dependent ('\r\n' on Windows, '\n' on
723 which is platform-dependent ('\r\n' on Windows, '\n' on
721 Unix, etc.) Specify None to write the lines as-is,
724 Unix, etc.) Specify None to write the lines as-is,
722 like file.writelines().
725 like file.writelines().
723
726
724 Use the keyword argument append=True to append lines to the
727 Use the keyword argument append=True to append lines to the
725 file. The default is to overwrite the file. Warning:
728 file. The default is to overwrite the file. Warning:
726 When you use this with Unicode data, if the encoding of the
729 When you use this with Unicode data, if the encoding of the
727 existing data in the file is different from the encoding
730 existing data in the file is different from the encoding
728 you specify with the encoding= parameter, the result is
731 you specify with the encoding= parameter, the result is
729 mixed-encoding data, which can really confuse someone trying
732 mixed-encoding data, which can really confuse someone trying
730 to read the file later.
733 to read the file later.
731 """
734 """
732 if append:
735 if append:
733 mode = 'ab'
736 mode = 'ab'
734 else:
737 else:
735 mode = 'wb'
738 mode = 'wb'
736 f = self.open(mode)
739 f = self.open(mode)
737 try:
740 try:
738 for line in lines:
741 for line in lines:
739 isUnicode = isinstance(line, unicode)
742 isUnicode = isinstance(line, unicode)
740 if linesep is not None:
743 if linesep is not None:
741 # Strip off any existing line-end and add the
744 # Strip off any existing line-end and add the
742 # specified linesep string.
745 # specified linesep string.
743 if isUnicode:
746 if isUnicode:
744 if line[-2:] in (u'\r\n', u'\x0d\x85'):
747 if line[-2:] in (u'\r\n', u'\x0d\x85'):
745 line = line[:-2]
748 line = line[:-2]
746 elif line[-1:] in (u'\r', u'\n',
749 elif line[-1:] in (u'\r', u'\n',
747 u'\x85', u'\u2028'):
750 u'\x85', u'\u2028'):
748 line = line[:-1]
751 line = line[:-1]
749 else:
752 else:
750 if line[-2:] == '\r\n':
753 if line[-2:] == '\r\n':
751 line = line[:-2]
754 line = line[:-2]
752 elif line[-1:] in ('\r', '\n'):
755 elif line[-1:] in ('\r', '\n'):
753 line = line[:-1]
756 line = line[:-1]
754 line += linesep
757 line += linesep
755 if isUnicode:
758 if isUnicode:
756 if encoding is None:
759 if encoding is None:
757 encoding = sys.getdefaultencoding()
760 encoding = sys.getdefaultencoding()
758 line = line.encode(encoding, errors)
761 line = line.encode(encoding, errors)
759 f.write(line)
762 f.write(line)
760 finally:
763 finally:
761 f.close()
764 f.close()
762
765
763 def read_md5(self):
766 def read_md5(self):
764 """ Calculate the md5 hash for this file.
767 """ Calculate the md5 hash for this file.
765
768
766 This reads through the entire file.
769 This reads through the entire file.
767 """
770 """
768 f = self.open('rb')
771 f = self.open('rb')
769 try:
772 try:
770 m = md5.new()
773 m = md5.new()
771 while True:
774 while True:
772 d = f.read(8192)
775 d = f.read(8192)
773 if not d:
776 if not d:
774 break
777 break
775 m.update(d)
778 m.update(d)
776 finally:
779 finally:
777 f.close()
780 f.close()
778 return m.digest()
781 return m.digest()
779
782
780 # --- Methods for querying the filesystem.
783 # --- Methods for querying the filesystem.
781
784
782 exists = os.path.exists
785 exists = os.path.exists
783 isdir = os.path.isdir
786 isdir = os.path.isdir
784 isfile = os.path.isfile
787 isfile = os.path.isfile
785 islink = os.path.islink
788 islink = os.path.islink
786 ismount = os.path.ismount
789 ismount = os.path.ismount
787
790
788 if hasattr(os.path, 'samefile'):
791 if hasattr(os.path, 'samefile'):
789 samefile = os.path.samefile
792 samefile = os.path.samefile
790
793
791 getatime = os.path.getatime
794 getatime = os.path.getatime
792 atime = property(
795 atime = property(
793 getatime, None, None,
796 getatime, None, None,
794 """ Last access time of the file. """)
797 """ Last access time of the file. """)
795
798
796 getmtime = os.path.getmtime
799 getmtime = os.path.getmtime
797 mtime = property(
800 mtime = property(
798 getmtime, None, None,
801 getmtime, None, None,
799 """ Last-modified time of the file. """)
802 """ Last-modified time of the file. """)
800
803
801 if hasattr(os.path, 'getctime'):
804 if hasattr(os.path, 'getctime'):
802 getctime = os.path.getctime
805 getctime = os.path.getctime
803 ctime = property(
806 ctime = property(
804 getctime, None, None,
807 getctime, None, None,
805 """ Creation time of the file. """)
808 """ Creation time of the file. """)
806
809
807 getsize = os.path.getsize
810 getsize = os.path.getsize
808 size = property(
811 size = property(
809 getsize, None, None,
812 getsize, None, None,
810 """ Size of the file, in bytes. """)
813 """ Size of the file, in bytes. """)
811
814
812 if hasattr(os, 'access'):
815 if hasattr(os, 'access'):
813 def access(self, mode):
816 def access(self, mode):
814 """ Return true if current user has access to this path.
817 """ Return true if current user has access to this path.
815
818
816 mode - One of the constants os.F_OK, os.R_OK, os.W_OK, os.X_OK
819 mode - One of the constants os.F_OK, os.R_OK, os.W_OK, os.X_OK
817 """
820 """
818 return os.access(self, mode)
821 return os.access(self, mode)
819
822
820 def stat(self):
823 def stat(self):
821 """ Perform a stat() system call on this path. """
824 """ Perform a stat() system call on this path. """
822 return os.stat(self)
825 return os.stat(self)
823
826
824 def lstat(self):
827 def lstat(self):
825 """ Like path.stat(), but do not follow symbolic links. """
828 """ Like path.stat(), but do not follow symbolic links. """
826 return os.lstat(self)
829 return os.lstat(self)
827
830
828 def get_owner(self):
831 def get_owner(self):
829 r""" Return the name of the owner of this file or directory.
832 r""" Return the name of the owner of this file or directory.
830
833
831 This follows symbolic links.
834 This follows symbolic links.
832
835
833 On Windows, this returns a name of the form ur'DOMAIN\User Name'.
836 On Windows, this returns a name of the form ur'DOMAIN\User Name'.
834 On Windows, a group can own a file or directory.
837 On Windows, a group can own a file or directory.
835 """
838 """
836 if os.name == 'nt':
839 if os.name == 'nt':
837 if win32security is None:
840 if win32security is None:
838 raise Exception("path.owner requires win32all to be installed")
841 raise Exception("path.owner requires win32all to be installed")
839 desc = win32security.GetFileSecurity(
842 desc = win32security.GetFileSecurity(
840 self, win32security.OWNER_SECURITY_INFORMATION)
843 self, win32security.OWNER_SECURITY_INFORMATION)
841 sid = desc.GetSecurityDescriptorOwner()
844 sid = desc.GetSecurityDescriptorOwner()
842 account, domain, typecode = win32security.LookupAccountSid(None, sid)
845 account, domain, typecode = win32security.LookupAccountSid(None, sid)
843 return domain + u'\\' + account
846 return domain + u'\\' + account
844 else:
847 else:
845 if pwd is None:
848 if pwd is None:
846 raise NotImplementedError("path.owner is not implemented on this platform.")
849 raise NotImplementedError("path.owner is not implemented on this platform.")
847 st = self.stat()
850 st = self.stat()
848 return pwd.getpwuid(st.st_uid).pw_name
851 return pwd.getpwuid(st.st_uid).pw_name
849
852
850 owner = property(
853 owner = property(
851 get_owner, None, None,
854 get_owner, None, None,
852 """ Name of the owner of this file or directory. """)
855 """ Name of the owner of this file or directory. """)
853
856
854 if hasattr(os, 'statvfs'):
857 if hasattr(os, 'statvfs'):
855 def statvfs(self):
858 def statvfs(self):
856 """ Perform a statvfs() system call on this path. """
859 """ Perform a statvfs() system call on this path. """
857 return os.statvfs(self)
860 return os.statvfs(self)
858
861
859 if hasattr(os, 'pathconf'):
862 if hasattr(os, 'pathconf'):
860 def pathconf(self, name):
863 def pathconf(self, name):
861 return os.pathconf(self, name)
864 return os.pathconf(self, name)
862
865
863
866
864 # --- Modifying operations on files and directories
867 # --- Modifying operations on files and directories
865
868
866 def utime(self, times):
869 def utime(self, times):
867 """ Set the access and modified times of this file. """
870 """ Set the access and modified times of this file. """
868 os.utime(self, times)
871 os.utime(self, times)
869
872
870 def chmod(self, mode):
873 def chmod(self, mode):
871 os.chmod(self, mode)
874 os.chmod(self, mode)
872
875
873 if hasattr(os, 'chown'):
876 if hasattr(os, 'chown'):
874 def chown(self, uid, gid):
877 def chown(self, uid, gid):
875 os.chown(self, uid, gid)
878 os.chown(self, uid, gid)
876
879
877 def rename(self, new):
880 def rename(self, new):
878 os.rename(self, new)
881 os.rename(self, new)
879
882
880 def renames(self, new):
883 def renames(self, new):
881 os.renames(self, new)
884 os.renames(self, new)
882
885
883
886
884 # --- Create/delete operations on directories
887 # --- Create/delete operations on directories
885
888
886 def mkdir(self, mode=0777):
889 def mkdir(self, mode=0777):
887 os.mkdir(self, mode)
890 os.mkdir(self, mode)
888
891
889 def makedirs(self, mode=0777):
892 def makedirs(self, mode=0777):
890 os.makedirs(self, mode)
893 os.makedirs(self, mode)
891
894
892 def rmdir(self):
895 def rmdir(self):
893 os.rmdir(self)
896 os.rmdir(self)
894
897
895 def removedirs(self):
898 def removedirs(self):
896 os.removedirs(self)
899 os.removedirs(self)
897
900
898
901
899 # --- Modifying operations on files
902 # --- Modifying operations on files
900
903
901 def touch(self):
904 def touch(self):
902 """ Set the access/modified times of this file to the current time.
905 """ Set the access/modified times of this file to the current time.
903 Create the file if it does not exist.
906 Create the file if it does not exist.
904 """
907 """
905 fd = os.open(self, os.O_WRONLY | os.O_CREAT, 0666)
908 fd = os.open(self, os.O_WRONLY | os.O_CREAT, 0666)
906 os.close(fd)
909 os.close(fd)
907 os.utime(self, None)
910 os.utime(self, None)
908
911
909 def remove(self):
912 def remove(self):
910 os.remove(self)
913 os.remove(self)
911
914
912 def unlink(self):
915 def unlink(self):
913 os.unlink(self)
916 os.unlink(self)
914
917
915
918
916 # --- Links
919 # --- Links
917
920
918 if hasattr(os, 'link'):
921 if hasattr(os, 'link'):
919 def link(self, newpath):
922 def link(self, newpath):
920 """ Create a hard link at 'newpath', pointing to this file. """
923 """ Create a hard link at 'newpath', pointing to this file. """
921 os.link(self, newpath)
924 os.link(self, newpath)
922
925
923 if hasattr(os, 'symlink'):
926 if hasattr(os, 'symlink'):
924 def symlink(self, newlink):
927 def symlink(self, newlink):
925 """ Create a symbolic link at 'newlink', pointing here. """
928 """ Create a symbolic link at 'newlink', pointing here. """
926 os.symlink(self, newlink)
929 os.symlink(self, newlink)
927
930
928 if hasattr(os, 'readlink'):
931 if hasattr(os, 'readlink'):
929 def readlink(self):
932 def readlink(self):
930 """ Return the path to which this symbolic link points.
933 """ Return the path to which this symbolic link points.
931
934
932 The result may be an absolute or a relative path.
935 The result may be an absolute or a relative path.
933 """
936 """
934 return self.__class__(os.readlink(self))
937 return self.__class__(os.readlink(self))
935
938
936 def readlinkabs(self):
939 def readlinkabs(self):
937 """ Return the path to which this symbolic link points.
940 """ Return the path to which this symbolic link points.
938
941
939 The result is always an absolute path.
942 The result is always an absolute path.
940 """
943 """
941 p = self.readlink()
944 p = self.readlink()
942 if p.isabs():
945 if p.isabs():
943 return p
946 return p
944 else:
947 else:
945 return (self.parent / p).abspath()
948 return (self.parent / p).abspath()
946
949
947
950
948 # --- High-level functions from shutil
951 # --- High-level functions from shutil
949
952
950 copyfile = shutil.copyfile
953 copyfile = shutil.copyfile
951 copymode = shutil.copymode
954 copymode = shutil.copymode
952 copystat = shutil.copystat
955 copystat = shutil.copystat
953 copy = shutil.copy
956 copy = shutil.copy
954 copy2 = shutil.copy2
957 copy2 = shutil.copy2
955 copytree = shutil.copytree
958 copytree = shutil.copytree
956 if hasattr(shutil, 'move'):
959 if hasattr(shutil, 'move'):
957 move = shutil.move
960 move = shutil.move
958 rmtree = shutil.rmtree
961 rmtree = shutil.rmtree
959
962
960
963
961 # --- Special stuff from os
964 # --- Special stuff from os
962
965
963 if hasattr(os, 'chroot'):
966 if hasattr(os, 'chroot'):
964 def chroot(self):
967 def chroot(self):
965 os.chroot(self)
968 os.chroot(self)
966
969
967 if hasattr(os, 'startfile'):
970 if hasattr(os, 'startfile'):
968 def startfile(self):
971 def startfile(self):
969 os.startfile(self)
972 os.startfile(self)
970
973
@@ -1,2681 +1,2683 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 IPython -- An enhanced Interactive Python
3 IPython -- An enhanced Interactive Python
4
4
5 Requires Python 2.3 or newer.
5 Requires Python 2.3 or newer.
6
6
7 This file contains all the classes and helper functions specific to IPython.
7 This file contains all the classes and helper functions specific to IPython.
8
8
9 """
9 """
10
10
11 #*****************************************************************************
11 #*****************************************************************************
12 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
12 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
13 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
13 # Copyright (C) 2001-2006 Fernando Perez. <fperez@colorado.edu>
14 #
14 #
15 # Distributed under the terms of the BSD License. The full license is in
15 # Distributed under the terms of the BSD License. The full license is in
16 # the file COPYING, distributed as part of this software.
16 # the file COPYING, distributed as part of this software.
17 #
17 #
18 # Note: this code originally subclassed code.InteractiveConsole from the
18 # Note: this code originally subclassed code.InteractiveConsole from the
19 # Python standard library. Over time, all of that class has been copied
19 # Python standard library. Over time, all of that class has been copied
20 # verbatim here for modifications which could not be accomplished by
20 # verbatim here for modifications which could not be accomplished by
21 # subclassing. At this point, there are no dependencies at all on the code
21 # subclassing. At this point, there are no dependencies at all on the code
22 # module anymore (it is not even imported). The Python License (sec. 2)
22 # module anymore (it is not even imported). The Python License (sec. 2)
23 # allows for this, but it's always nice to acknowledge credit where credit is
23 # allows for this, but it's always nice to acknowledge credit where credit is
24 # due.
24 # due.
25 #*****************************************************************************
25 #*****************************************************************************
26
26
27 #****************************************************************************
27 #****************************************************************************
28 # Modules and globals
28 # Modules and globals
29
29
30 from IPython import Release
30 from IPython import Release
31 __author__ = '%s <%s>\n%s <%s>' % \
31 __author__ = '%s <%s>\n%s <%s>' % \
32 ( Release.authors['Janko'] + Release.authors['Fernando'] )
32 ( Release.authors['Janko'] + Release.authors['Fernando'] )
33 __license__ = Release.license
33 __license__ = Release.license
34 __version__ = Release.version
34 __version__ = Release.version
35
35
36 # Python standard modules
36 # Python standard modules
37 import __main__
37 import __main__
38 import __builtin__
38 import __builtin__
39 import StringIO
39 import StringIO
40 import bdb
40 import bdb
41 import cPickle as pickle
41 import cPickle as pickle
42 import codeop
42 import codeop
43 import exceptions
43 import exceptions
44 import glob
44 import glob
45 import inspect
45 import inspect
46 import keyword
46 import keyword
47 import new
47 import new
48 import os
48 import os
49 import pydoc
49 import pydoc
50 import re
50 import re
51 import shutil
51 import shutil
52 import string
52 import string
53 import sys
53 import sys
54 import tempfile
54 import tempfile
55 import traceback
55 import traceback
56 import types
56 import types
57 import warnings
58 warnings.filterwarnings('ignore', r'.*sets module*')
57 from sets import Set
59 from sets import Set
58 from pprint import pprint, pformat
60 from pprint import pprint, pformat
59
61
60 # IPython's own modules
62 # IPython's own modules
61 #import IPython
63 #import IPython
62 from IPython import Debugger,OInspect,PyColorize,ultraTB
64 from IPython import Debugger,OInspect,PyColorize,ultraTB
63 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
65 from IPython.ColorANSI import ColorScheme,ColorSchemeTable # too long names
64 from IPython.Extensions import pickleshare
66 from IPython.Extensions import pickleshare
65 from IPython.FakeModule import FakeModule
67 from IPython.FakeModule import FakeModule
66 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
68 from IPython.Itpl import Itpl,itpl,printpl,ItplNS,itplns
67 from IPython.Logger import Logger
69 from IPython.Logger import Logger
68 from IPython.Magic import Magic
70 from IPython.Magic import Magic
69 from IPython.Prompts import CachedOutput
71 from IPython.Prompts import CachedOutput
70 from IPython.ipstruct import Struct
72 from IPython.ipstruct import Struct
71 from IPython.background_jobs import BackgroundJobManager
73 from IPython.background_jobs import BackgroundJobManager
72 from IPython.usage import cmd_line_usage,interactive_usage
74 from IPython.usage import cmd_line_usage,interactive_usage
73 from IPython.genutils import *
75 from IPython.genutils import *
74 from IPython.strdispatch import StrDispatch
76 from IPython.strdispatch import StrDispatch
75 import IPython.ipapi
77 import IPython.ipapi
76 import IPython.history
78 import IPython.history
77 import IPython.prefilter as prefilter
79 import IPython.prefilter as prefilter
78 import IPython.shadowns
80 import IPython.shadowns
79 # Globals
81 # Globals
80
82
81 # store the builtin raw_input globally, and use this always, in case user code
83 # store the builtin raw_input globally, and use this always, in case user code
82 # overwrites it (like wx.py.PyShell does)
84 # overwrites it (like wx.py.PyShell does)
83 raw_input_original = raw_input
85 raw_input_original = raw_input
84
86
85 # compiled regexps for autoindent management
87 # compiled regexps for autoindent management
86 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
88 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
87
89
88
90
89 #****************************************************************************
91 #****************************************************************************
90 # Some utility function definitions
92 # Some utility function definitions
91
93
92 ini_spaces_re = re.compile(r'^(\s+)')
94 ini_spaces_re = re.compile(r'^(\s+)')
93
95
94 def num_ini_spaces(strng):
96 def num_ini_spaces(strng):
95 """Return the number of initial spaces in a string"""
97 """Return the number of initial spaces in a string"""
96
98
97 ini_spaces = ini_spaces_re.match(strng)
99 ini_spaces = ini_spaces_re.match(strng)
98 if ini_spaces:
100 if ini_spaces:
99 return ini_spaces.end()
101 return ini_spaces.end()
100 else:
102 else:
101 return 0
103 return 0
102
104
103 def softspace(file, newvalue):
105 def softspace(file, newvalue):
104 """Copied from code.py, to remove the dependency"""
106 """Copied from code.py, to remove the dependency"""
105
107
106 oldvalue = 0
108 oldvalue = 0
107 try:
109 try:
108 oldvalue = file.softspace
110 oldvalue = file.softspace
109 except AttributeError:
111 except AttributeError:
110 pass
112 pass
111 try:
113 try:
112 file.softspace = newvalue
114 file.softspace = newvalue
113 except (AttributeError, TypeError):
115 except (AttributeError, TypeError):
114 # "attribute-less object" or "read-only attributes"
116 # "attribute-less object" or "read-only attributes"
115 pass
117 pass
116 return oldvalue
118 return oldvalue
117
119
118
120
119 #****************************************************************************
121 #****************************************************************************
120 # Local use exceptions
122 # Local use exceptions
121 class SpaceInInput(exceptions.Exception): pass
123 class SpaceInInput(exceptions.Exception): pass
122
124
123
125
124 #****************************************************************************
126 #****************************************************************************
125 # Local use classes
127 # Local use classes
126 class Bunch: pass
128 class Bunch: pass
127
129
128 class Undefined: pass
130 class Undefined: pass
129
131
130 class Quitter(object):
132 class Quitter(object):
131 """Simple class to handle exit, similar to Python 2.5's.
133 """Simple class to handle exit, similar to Python 2.5's.
132
134
133 It handles exiting in an ipython-safe manner, which the one in Python 2.5
135 It handles exiting in an ipython-safe manner, which the one in Python 2.5
134 doesn't do (obviously, since it doesn't know about ipython)."""
136 doesn't do (obviously, since it doesn't know about ipython)."""
135
137
136 def __init__(self,shell,name):
138 def __init__(self,shell,name):
137 self.shell = shell
139 self.shell = shell
138 self.name = name
140 self.name = name
139
141
140 def __repr__(self):
142 def __repr__(self):
141 return 'Type %s() to exit.' % self.name
143 return 'Type %s() to exit.' % self.name
142 __str__ = __repr__
144 __str__ = __repr__
143
145
144 def __call__(self):
146 def __call__(self):
145 self.shell.exit()
147 self.shell.exit()
146
148
147 class InputList(list):
149 class InputList(list):
148 """Class to store user input.
150 """Class to store user input.
149
151
150 It's basically a list, but slices return a string instead of a list, thus
152 It's basically a list, but slices return a string instead of a list, thus
151 allowing things like (assuming 'In' is an instance):
153 allowing things like (assuming 'In' is an instance):
152
154
153 exec In[4:7]
155 exec In[4:7]
154
156
155 or
157 or
156
158
157 exec In[5:9] + In[14] + In[21:25]"""
159 exec In[5:9] + In[14] + In[21:25]"""
158
160
159 def __getslice__(self,i,j):
161 def __getslice__(self,i,j):
160 return ''.join(list.__getslice__(self,i,j))
162 return ''.join(list.__getslice__(self,i,j))
161
163
162 class SyntaxTB(ultraTB.ListTB):
164 class SyntaxTB(ultraTB.ListTB):
163 """Extension which holds some state: the last exception value"""
165 """Extension which holds some state: the last exception value"""
164
166
165 def __init__(self,color_scheme = 'NoColor'):
167 def __init__(self,color_scheme = 'NoColor'):
166 ultraTB.ListTB.__init__(self,color_scheme)
168 ultraTB.ListTB.__init__(self,color_scheme)
167 self.last_syntax_error = None
169 self.last_syntax_error = None
168
170
169 def __call__(self, etype, value, elist):
171 def __call__(self, etype, value, elist):
170 self.last_syntax_error = value
172 self.last_syntax_error = value
171 ultraTB.ListTB.__call__(self,etype,value,elist)
173 ultraTB.ListTB.__call__(self,etype,value,elist)
172
174
173 def clear_err_state(self):
175 def clear_err_state(self):
174 """Return the current error state and clear it"""
176 """Return the current error state and clear it"""
175 e = self.last_syntax_error
177 e = self.last_syntax_error
176 self.last_syntax_error = None
178 self.last_syntax_error = None
177 return e
179 return e
178
180
179 #****************************************************************************
181 #****************************************************************************
180 # Main IPython class
182 # Main IPython class
181
183
182 # FIXME: the Magic class is a mixin for now, and will unfortunately remain so
184 # FIXME: the Magic class is a mixin for now, and will unfortunately remain so
183 # until a full rewrite is made. I've cleaned all cross-class uses of
185 # until a full rewrite is made. I've cleaned all cross-class uses of
184 # attributes and methods, but too much user code out there relies on the
186 # attributes and methods, but too much user code out there relies on the
185 # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
187 # equlity %foo == __IP.magic_foo, so I can't actually remove the mixin usage.
186 #
188 #
187 # But at least now, all the pieces have been separated and we could, in
189 # But at least now, all the pieces have been separated and we could, in
188 # principle, stop using the mixin. This will ease the transition to the
190 # principle, stop using the mixin. This will ease the transition to the
189 # chainsaw branch.
191 # chainsaw branch.
190
192
191 # For reference, the following is the list of 'self.foo' uses in the Magic
193 # For reference, the following is the list of 'self.foo' uses in the Magic
192 # class as of 2005-12-28. These are names we CAN'T use in the main ipython
194 # class as of 2005-12-28. These are names we CAN'T use in the main ipython
193 # class, to prevent clashes.
195 # class, to prevent clashes.
194
196
195 # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
197 # ['self.__class__', 'self.__dict__', 'self._inspect', 'self._ofind',
196 # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
198 # 'self.arg_err', 'self.extract_input', 'self.format_', 'self.lsmagic',
197 # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
199 # 'self.magic_', 'self.options_table', 'self.parse', 'self.shell',
198 # 'self.value']
200 # 'self.value']
199
201
200 class InteractiveShell(object,Magic):
202 class InteractiveShell(object,Magic):
201 """An enhanced console for Python."""
203 """An enhanced console for Python."""
202
204
203 # class attribute to indicate whether the class supports threads or not.
205 # class attribute to indicate whether the class supports threads or not.
204 # Subclasses with thread support should override this as needed.
206 # Subclasses with thread support should override this as needed.
205 isthreaded = False
207 isthreaded = False
206
208
207 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
209 def __init__(self,name,usage=None,rc=Struct(opts=None,args=None),
208 user_ns = None,user_global_ns=None,banner2='',
210 user_ns = None,user_global_ns=None,banner2='',
209 custom_exceptions=((),None),embedded=False):
211 custom_exceptions=((),None),embedded=False):
210
212
211 # log system
213 # log system
212 self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate')
214 self.logger = Logger(self,logfname='ipython_log.py',logmode='rotate')
213
215
214 # some minimal strict typechecks. For some core data structures, I
216 # some minimal strict typechecks. For some core data structures, I
215 # want actual basic python types, not just anything that looks like
217 # want actual basic python types, not just anything that looks like
216 # one. This is especially true for namespaces.
218 # one. This is especially true for namespaces.
217 for ns in (user_ns,user_global_ns):
219 for ns in (user_ns,user_global_ns):
218 if ns is not None and type(ns) != types.DictType:
220 if ns is not None and type(ns) != types.DictType:
219 raise TypeError,'namespace must be a dictionary'
221 raise TypeError,'namespace must be a dictionary'
220 # Job manager (for jobs run as background threads)
222 # Job manager (for jobs run as background threads)
221 self.jobs = BackgroundJobManager()
223 self.jobs = BackgroundJobManager()
222
224
223 # Store the actual shell's name
225 # Store the actual shell's name
224 self.name = name
226 self.name = name
225 self.more = False
227 self.more = False
226
228
227 # We need to know whether the instance is meant for embedding, since
229 # We need to know whether the instance is meant for embedding, since
228 # global/local namespaces need to be handled differently in that case
230 # global/local namespaces need to be handled differently in that case
229 self.embedded = embedded
231 self.embedded = embedded
230 if embedded:
232 if embedded:
231 # Control variable so users can, from within the embedded instance,
233 # Control variable so users can, from within the embedded instance,
232 # permanently deactivate it.
234 # permanently deactivate it.
233 self.embedded_active = True
235 self.embedded_active = True
234
236
235 # command compiler
237 # command compiler
236 self.compile = codeop.CommandCompiler()
238 self.compile = codeop.CommandCompiler()
237
239
238 # User input buffer
240 # User input buffer
239 self.buffer = []
241 self.buffer = []
240
242
241 # Default name given in compilation of code
243 # Default name given in compilation of code
242 self.filename = '<ipython console>'
244 self.filename = '<ipython console>'
243
245
244 # Install our own quitter instead of the builtins. For python2.3-2.4,
246 # Install our own quitter instead of the builtins. For python2.3-2.4,
245 # this brings in behavior like 2.5, and for 2.5 it's identical.
247 # this brings in behavior like 2.5, and for 2.5 it's identical.
246 __builtin__.exit = Quitter(self,'exit')
248 __builtin__.exit = Quitter(self,'exit')
247 __builtin__.quit = Quitter(self,'quit')
249 __builtin__.quit = Quitter(self,'quit')
248
250
249 # Make an empty namespace, which extension writers can rely on both
251 # Make an empty namespace, which extension writers can rely on both
250 # existing and NEVER being used by ipython itself. This gives them a
252 # existing and NEVER being used by ipython itself. This gives them a
251 # convenient location for storing additional information and state
253 # convenient location for storing additional information and state
252 # their extensions may require, without fear of collisions with other
254 # their extensions may require, without fear of collisions with other
253 # ipython names that may develop later.
255 # ipython names that may develop later.
254 self.meta = Struct()
256 self.meta = Struct()
255
257
256 # Create the namespace where the user will operate. user_ns is
258 # Create the namespace where the user will operate. user_ns is
257 # normally the only one used, and it is passed to the exec calls as
259 # normally the only one used, and it is passed to the exec calls as
258 # the locals argument. But we do carry a user_global_ns namespace
260 # the locals argument. But we do carry a user_global_ns namespace
259 # given as the exec 'globals' argument, This is useful in embedding
261 # given as the exec 'globals' argument, This is useful in embedding
260 # situations where the ipython shell opens in a context where the
262 # situations where the ipython shell opens in a context where the
261 # distinction between locals and globals is meaningful.
263 # distinction between locals and globals is meaningful.
262
264
263 # FIXME. For some strange reason, __builtins__ is showing up at user
265 # FIXME. For some strange reason, __builtins__ is showing up at user
264 # level as a dict instead of a module. This is a manual fix, but I
266 # level as a dict instead of a module. This is a manual fix, but I
265 # should really track down where the problem is coming from. Alex
267 # should really track down where the problem is coming from. Alex
266 # Schmolck reported this problem first.
268 # Schmolck reported this problem first.
267
269
268 # A useful post by Alex Martelli on this topic:
270 # A useful post by Alex Martelli on this topic:
269 # Re: inconsistent value from __builtins__
271 # Re: inconsistent value from __builtins__
270 # Von: Alex Martelli <aleaxit@yahoo.com>
272 # Von: Alex Martelli <aleaxit@yahoo.com>
271 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
273 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
272 # Gruppen: comp.lang.python
274 # Gruppen: comp.lang.python
273
275
274 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
276 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
275 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
277 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
276 # > <type 'dict'>
278 # > <type 'dict'>
277 # > >>> print type(__builtins__)
279 # > >>> print type(__builtins__)
278 # > <type 'module'>
280 # > <type 'module'>
279 # > Is this difference in return value intentional?
281 # > Is this difference in return value intentional?
280
282
281 # Well, it's documented that '__builtins__' can be either a dictionary
283 # Well, it's documented that '__builtins__' can be either a dictionary
282 # or a module, and it's been that way for a long time. Whether it's
284 # or a module, and it's been that way for a long time. Whether it's
283 # intentional (or sensible), I don't know. In any case, the idea is
285 # intentional (or sensible), I don't know. In any case, the idea is
284 # that if you need to access the built-in namespace directly, you
286 # that if you need to access the built-in namespace directly, you
285 # should start with "import __builtin__" (note, no 's') which will
287 # should start with "import __builtin__" (note, no 's') which will
286 # definitely give you a module. Yeah, it's somewhat confusing:-(.
288 # definitely give you a module. Yeah, it's somewhat confusing:-(.
287
289
288 # These routines return properly built dicts as needed by the rest of
290 # These routines return properly built dicts as needed by the rest of
289 # the code, and can also be used by extension writers to generate
291 # the code, and can also be used by extension writers to generate
290 # properly initialized namespaces.
292 # properly initialized namespaces.
291 user_ns = IPython.ipapi.make_user_ns(user_ns)
293 user_ns = IPython.ipapi.make_user_ns(user_ns)
292 user_global_ns = IPython.ipapi.make_user_global_ns(user_global_ns)
294 user_global_ns = IPython.ipapi.make_user_global_ns(user_global_ns)
293
295
294 # Assign namespaces
296 # Assign namespaces
295 # This is the namespace where all normal user variables live
297 # This is the namespace where all normal user variables live
296 self.user_ns = user_ns
298 self.user_ns = user_ns
297 # Embedded instances require a separate namespace for globals.
299 # Embedded instances require a separate namespace for globals.
298 # Normally this one is unused by non-embedded instances.
300 # Normally this one is unused by non-embedded instances.
299 self.user_global_ns = user_global_ns
301 self.user_global_ns = user_global_ns
300 # A namespace to keep track of internal data structures to prevent
302 # A namespace to keep track of internal data structures to prevent
301 # them from cluttering user-visible stuff. Will be updated later
303 # them from cluttering user-visible stuff. Will be updated later
302 self.internal_ns = {}
304 self.internal_ns = {}
303
305
304 # Namespace of system aliases. Each entry in the alias
306 # Namespace of system aliases. Each entry in the alias
305 # table must be a 2-tuple of the form (N,name), where N is the number
307 # table must be a 2-tuple of the form (N,name), where N is the number
306 # of positional arguments of the alias.
308 # of positional arguments of the alias.
307 self.alias_table = {}
309 self.alias_table = {}
308
310
309 # A table holding all the namespaces IPython deals with, so that
311 # A table holding all the namespaces IPython deals with, so that
310 # introspection facilities can search easily.
312 # introspection facilities can search easily.
311 self.ns_table = {'user':user_ns,
313 self.ns_table = {'user':user_ns,
312 'user_global':user_global_ns,
314 'user_global':user_global_ns,
313 'alias':self.alias_table,
315 'alias':self.alias_table,
314 'internal':self.internal_ns,
316 'internal':self.internal_ns,
315 'builtin':__builtin__.__dict__
317 'builtin':__builtin__.__dict__
316 }
318 }
317 # The user namespace MUST have a pointer to the shell itself.
319 # The user namespace MUST have a pointer to the shell itself.
318 self.user_ns[name] = self
320 self.user_ns[name] = self
319
321
320 # We need to insert into sys.modules something that looks like a
322 # We need to insert into sys.modules something that looks like a
321 # module but which accesses the IPython namespace, for shelve and
323 # module but which accesses the IPython namespace, for shelve and
322 # pickle to work interactively. Normally they rely on getting
324 # pickle to work interactively. Normally they rely on getting
323 # everything out of __main__, but for embedding purposes each IPython
325 # everything out of __main__, but for embedding purposes each IPython
324 # instance has its own private namespace, so we can't go shoving
326 # instance has its own private namespace, so we can't go shoving
325 # everything into __main__.
327 # everything into __main__.
326
328
327 # note, however, that we should only do this for non-embedded
329 # note, however, that we should only do this for non-embedded
328 # ipythons, which really mimic the __main__.__dict__ with their own
330 # ipythons, which really mimic the __main__.__dict__ with their own
329 # namespace. Embedded instances, on the other hand, should not do
331 # namespace. Embedded instances, on the other hand, should not do
330 # this because they need to manage the user local/global namespaces
332 # this because they need to manage the user local/global namespaces
331 # only, but they live within a 'normal' __main__ (meaning, they
333 # only, but they live within a 'normal' __main__ (meaning, they
332 # shouldn't overtake the execution environment of the script they're
334 # shouldn't overtake the execution environment of the script they're
333 # embedded in).
335 # embedded in).
334
336
335 if not embedded:
337 if not embedded:
336 try:
338 try:
337 main_name = self.user_ns['__name__']
339 main_name = self.user_ns['__name__']
338 except KeyError:
340 except KeyError:
339 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
341 raise KeyError,'user_ns dictionary MUST have a "__name__" key'
340 else:
342 else:
341 #print "pickle hack in place" # dbg
343 #print "pickle hack in place" # dbg
342 #print 'main_name:',main_name # dbg
344 #print 'main_name:',main_name # dbg
343 sys.modules[main_name] = FakeModule(self.user_ns)
345 sys.modules[main_name] = FakeModule(self.user_ns)
344
346
345 # Now that FakeModule produces a real module, we've run into a nasty
347 # Now that FakeModule produces a real module, we've run into a nasty
346 # problem: after script execution (via %run), the module where the user
348 # problem: after script execution (via %run), the module where the user
347 # code ran is deleted. Now that this object is a true module (needed
349 # code ran is deleted. Now that this object is a true module (needed
348 # so docetst and other tools work correctly), the Python module
350 # so docetst and other tools work correctly), the Python module
349 # teardown mechanism runs over it, and sets to None every variable
351 # teardown mechanism runs over it, and sets to None every variable
350 # present in that module. This means that later calls to functions
352 # present in that module. This means that later calls to functions
351 # defined in the script (which have become interactively visible after
353 # defined in the script (which have become interactively visible after
352 # script exit) fail, because they hold references to objects that have
354 # script exit) fail, because they hold references to objects that have
353 # become overwritten into None. The only solution I see right now is
355 # become overwritten into None. The only solution I see right now is
354 # to protect every FakeModule used by %run by holding an internal
356 # to protect every FakeModule used by %run by holding an internal
355 # reference to it. This private list will be used for that. The
357 # reference to it. This private list will be used for that. The
356 # %reset command will flush it as well.
358 # %reset command will flush it as well.
357 self._user_main_modules = []
359 self._user_main_modules = []
358
360
359 # List of input with multi-line handling.
361 # List of input with multi-line handling.
360 # Fill its zero entry, user counter starts at 1
362 # Fill its zero entry, user counter starts at 1
361 self.input_hist = InputList(['\n'])
363 self.input_hist = InputList(['\n'])
362 # This one will hold the 'raw' input history, without any
364 # This one will hold the 'raw' input history, without any
363 # pre-processing. This will allow users to retrieve the input just as
365 # pre-processing. This will allow users to retrieve the input just as
364 # it was exactly typed in by the user, with %hist -r.
366 # it was exactly typed in by the user, with %hist -r.
365 self.input_hist_raw = InputList(['\n'])
367 self.input_hist_raw = InputList(['\n'])
366
368
367 # list of visited directories
369 # list of visited directories
368 try:
370 try:
369 self.dir_hist = [os.getcwd()]
371 self.dir_hist = [os.getcwd()]
370 except OSError:
372 except OSError:
371 self.dir_hist = []
373 self.dir_hist = []
372
374
373 # dict of output history
375 # dict of output history
374 self.output_hist = {}
376 self.output_hist = {}
375
377
376 # Get system encoding at startup time. Certain terminals (like Emacs
378 # Get system encoding at startup time. Certain terminals (like Emacs
377 # under Win32 have it set to None, and we need to have a known valid
379 # under Win32 have it set to None, and we need to have a known valid
378 # encoding to use in the raw_input() method
380 # encoding to use in the raw_input() method
379 try:
381 try:
380 self.stdin_encoding = sys.stdin.encoding or 'ascii'
382 self.stdin_encoding = sys.stdin.encoding or 'ascii'
381 except AttributeError:
383 except AttributeError:
382 self.stdin_encoding = 'ascii'
384 self.stdin_encoding = 'ascii'
383
385
384 # dict of things NOT to alias (keywords, builtins and some magics)
386 # dict of things NOT to alias (keywords, builtins and some magics)
385 no_alias = {}
387 no_alias = {}
386 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
388 no_alias_magics = ['cd','popd','pushd','dhist','alias','unalias']
387 for key in keyword.kwlist + no_alias_magics:
389 for key in keyword.kwlist + no_alias_magics:
388 no_alias[key] = 1
390 no_alias[key] = 1
389 no_alias.update(__builtin__.__dict__)
391 no_alias.update(__builtin__.__dict__)
390 self.no_alias = no_alias
392 self.no_alias = no_alias
391
393
392 # make global variables for user access to these
394 # make global variables for user access to these
393 self.user_ns['_ih'] = self.input_hist
395 self.user_ns['_ih'] = self.input_hist
394 self.user_ns['_oh'] = self.output_hist
396 self.user_ns['_oh'] = self.output_hist
395 self.user_ns['_dh'] = self.dir_hist
397 self.user_ns['_dh'] = self.dir_hist
396
398
397 # user aliases to input and output histories
399 # user aliases to input and output histories
398 self.user_ns['In'] = self.input_hist
400 self.user_ns['In'] = self.input_hist
399 self.user_ns['Out'] = self.output_hist
401 self.user_ns['Out'] = self.output_hist
400
402
401 self.user_ns['_sh'] = IPython.shadowns
403 self.user_ns['_sh'] = IPython.shadowns
402 # Object variable to store code object waiting execution. This is
404 # Object variable to store code object waiting execution. This is
403 # used mainly by the multithreaded shells, but it can come in handy in
405 # used mainly by the multithreaded shells, but it can come in handy in
404 # other situations. No need to use a Queue here, since it's a single
406 # other situations. No need to use a Queue here, since it's a single
405 # item which gets cleared once run.
407 # item which gets cleared once run.
406 self.code_to_run = None
408 self.code_to_run = None
407
409
408 # escapes for automatic behavior on the command line
410 # escapes for automatic behavior on the command line
409 self.ESC_SHELL = '!'
411 self.ESC_SHELL = '!'
410 self.ESC_SH_CAP = '!!'
412 self.ESC_SH_CAP = '!!'
411 self.ESC_HELP = '?'
413 self.ESC_HELP = '?'
412 self.ESC_MAGIC = '%'
414 self.ESC_MAGIC = '%'
413 self.ESC_QUOTE = ','
415 self.ESC_QUOTE = ','
414 self.ESC_QUOTE2 = ';'
416 self.ESC_QUOTE2 = ';'
415 self.ESC_PAREN = '/'
417 self.ESC_PAREN = '/'
416
418
417 # And their associated handlers
419 # And their associated handlers
418 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
420 self.esc_handlers = {self.ESC_PAREN : self.handle_auto,
419 self.ESC_QUOTE : self.handle_auto,
421 self.ESC_QUOTE : self.handle_auto,
420 self.ESC_QUOTE2 : self.handle_auto,
422 self.ESC_QUOTE2 : self.handle_auto,
421 self.ESC_MAGIC : self.handle_magic,
423 self.ESC_MAGIC : self.handle_magic,
422 self.ESC_HELP : self.handle_help,
424 self.ESC_HELP : self.handle_help,
423 self.ESC_SHELL : self.handle_shell_escape,
425 self.ESC_SHELL : self.handle_shell_escape,
424 self.ESC_SH_CAP : self.handle_shell_escape,
426 self.ESC_SH_CAP : self.handle_shell_escape,
425 }
427 }
426
428
427 # class initializations
429 # class initializations
428 Magic.__init__(self,self)
430 Magic.__init__(self,self)
429
431
430 # Python source parser/formatter for syntax highlighting
432 # Python source parser/formatter for syntax highlighting
431 pyformat = PyColorize.Parser().format
433 pyformat = PyColorize.Parser().format
432 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
434 self.pycolorize = lambda src: pyformat(src,'str',self.rc['colors'])
433
435
434 # hooks holds pointers used for user-side customizations
436 # hooks holds pointers used for user-side customizations
435 self.hooks = Struct()
437 self.hooks = Struct()
436
438
437 self.strdispatchers = {}
439 self.strdispatchers = {}
438
440
439 # Set all default hooks, defined in the IPython.hooks module.
441 # Set all default hooks, defined in the IPython.hooks module.
440 hooks = IPython.hooks
442 hooks = IPython.hooks
441 for hook_name in hooks.__all__:
443 for hook_name in hooks.__all__:
442 # default hooks have priority 100, i.e. low; user hooks should have
444 # default hooks have priority 100, i.e. low; user hooks should have
443 # 0-100 priority
445 # 0-100 priority
444 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
446 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
445 #print "bound hook",hook_name
447 #print "bound hook",hook_name
446
448
447 # Flag to mark unconditional exit
449 # Flag to mark unconditional exit
448 self.exit_now = False
450 self.exit_now = False
449
451
450 self.usage_min = """\
452 self.usage_min = """\
451 An enhanced console for Python.
453 An enhanced console for Python.
452 Some of its features are:
454 Some of its features are:
453 - Readline support if the readline library is present.
455 - Readline support if the readline library is present.
454 - Tab completion in the local namespace.
456 - Tab completion in the local namespace.
455 - Logging of input, see command-line options.
457 - Logging of input, see command-line options.
456 - System shell escape via ! , eg !ls.
458 - System shell escape via ! , eg !ls.
457 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
459 - Magic commands, starting with a % (like %ls, %pwd, %cd, etc.)
458 - Keeps track of locally defined variables via %who, %whos.
460 - Keeps track of locally defined variables via %who, %whos.
459 - Show object information with a ? eg ?x or x? (use ?? for more info).
461 - Show object information with a ? eg ?x or x? (use ?? for more info).
460 """
462 """
461 if usage: self.usage = usage
463 if usage: self.usage = usage
462 else: self.usage = self.usage_min
464 else: self.usage = self.usage_min
463
465
464 # Storage
466 # Storage
465 self.rc = rc # This will hold all configuration information
467 self.rc = rc # This will hold all configuration information
466 self.pager = 'less'
468 self.pager = 'less'
467 # temporary files used for various purposes. Deleted at exit.
469 # temporary files used for various purposes. Deleted at exit.
468 self.tempfiles = []
470 self.tempfiles = []
469
471
470 # Keep track of readline usage (later set by init_readline)
472 # Keep track of readline usage (later set by init_readline)
471 self.has_readline = False
473 self.has_readline = False
472
474
473 # template for logfile headers. It gets resolved at runtime by the
475 # template for logfile headers. It gets resolved at runtime by the
474 # logstart method.
476 # logstart method.
475 self.loghead_tpl = \
477 self.loghead_tpl = \
476 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
478 """#log# Automatic Logger file. *** THIS MUST BE THE FIRST LINE ***
477 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
479 #log# DO NOT CHANGE THIS LINE OR THE TWO BELOW
478 #log# opts = %s
480 #log# opts = %s
479 #log# args = %s
481 #log# args = %s
480 #log# It is safe to make manual edits below here.
482 #log# It is safe to make manual edits below here.
481 #log#-----------------------------------------------------------------------
483 #log#-----------------------------------------------------------------------
482 """
484 """
483 # for pushd/popd management
485 # for pushd/popd management
484 try:
486 try:
485 self.home_dir = get_home_dir()
487 self.home_dir = get_home_dir()
486 except HomeDirError,msg:
488 except HomeDirError,msg:
487 fatal(msg)
489 fatal(msg)
488
490
489 self.dir_stack = []
491 self.dir_stack = []
490
492
491 # Functions to call the underlying shell.
493 # Functions to call the underlying shell.
492
494
493 # The first is similar to os.system, but it doesn't return a value,
495 # The first is similar to os.system, but it doesn't return a value,
494 # and it allows interpolation of variables in the user's namespace.
496 # and it allows interpolation of variables in the user's namespace.
495 self.system = lambda cmd: \
497 self.system = lambda cmd: \
496 self.hooks.shell_hook(self.var_expand(cmd,depth=2))
498 self.hooks.shell_hook(self.var_expand(cmd,depth=2))
497
499
498 # These are for getoutput and getoutputerror:
500 # These are for getoutput and getoutputerror:
499 self.getoutput = lambda cmd: \
501 self.getoutput = lambda cmd: \
500 getoutput(self.var_expand(cmd,depth=2),
502 getoutput(self.var_expand(cmd,depth=2),
501 header=self.rc.system_header,
503 header=self.rc.system_header,
502 verbose=self.rc.system_verbose)
504 verbose=self.rc.system_verbose)
503
505
504 self.getoutputerror = lambda cmd: \
506 self.getoutputerror = lambda cmd: \
505 getoutputerror(self.var_expand(cmd,depth=2),
507 getoutputerror(self.var_expand(cmd,depth=2),
506 header=self.rc.system_header,
508 header=self.rc.system_header,
507 verbose=self.rc.system_verbose)
509 verbose=self.rc.system_verbose)
508
510
509
511
510 # keep track of where we started running (mainly for crash post-mortem)
512 # keep track of where we started running (mainly for crash post-mortem)
511 self.starting_dir = os.getcwd()
513 self.starting_dir = os.getcwd()
512
514
513 # Various switches which can be set
515 # Various switches which can be set
514 self.CACHELENGTH = 5000 # this is cheap, it's just text
516 self.CACHELENGTH = 5000 # this is cheap, it's just text
515 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
517 self.BANNER = "Python %(version)s on %(platform)s\n" % sys.__dict__
516 self.banner2 = banner2
518 self.banner2 = banner2
517
519
518 # TraceBack handlers:
520 # TraceBack handlers:
519
521
520 # Syntax error handler.
522 # Syntax error handler.
521 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
523 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
522
524
523 # The interactive one is initialized with an offset, meaning we always
525 # The interactive one is initialized with an offset, meaning we always
524 # want to remove the topmost item in the traceback, which is our own
526 # want to remove the topmost item in the traceback, which is our own
525 # internal code. Valid modes: ['Plain','Context','Verbose']
527 # internal code. Valid modes: ['Plain','Context','Verbose']
526 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
528 self.InteractiveTB = ultraTB.AutoFormattedTB(mode = 'Plain',
527 color_scheme='NoColor',
529 color_scheme='NoColor',
528 tb_offset = 1)
530 tb_offset = 1)
529
531
530 # IPython itself shouldn't crash. This will produce a detailed
532 # IPython itself shouldn't crash. This will produce a detailed
531 # post-mortem if it does. But we only install the crash handler for
533 # post-mortem if it does. But we only install the crash handler for
532 # non-threaded shells, the threaded ones use a normal verbose reporter
534 # non-threaded shells, the threaded ones use a normal verbose reporter
533 # and lose the crash handler. This is because exceptions in the main
535 # and lose the crash handler. This is because exceptions in the main
534 # thread (such as in GUI code) propagate directly to sys.excepthook,
536 # thread (such as in GUI code) propagate directly to sys.excepthook,
535 # and there's no point in printing crash dumps for every user exception.
537 # and there's no point in printing crash dumps for every user exception.
536 if self.isthreaded:
538 if self.isthreaded:
537 ipCrashHandler = ultraTB.FormattedTB()
539 ipCrashHandler = ultraTB.FormattedTB()
538 else:
540 else:
539 from IPython import CrashHandler
541 from IPython import CrashHandler
540 ipCrashHandler = CrashHandler.IPythonCrashHandler(self)
542 ipCrashHandler = CrashHandler.IPythonCrashHandler(self)
541 self.set_crash_handler(ipCrashHandler)
543 self.set_crash_handler(ipCrashHandler)
542
544
543 # and add any custom exception handlers the user may have specified
545 # and add any custom exception handlers the user may have specified
544 self.set_custom_exc(*custom_exceptions)
546 self.set_custom_exc(*custom_exceptions)
545
547
546 # indentation management
548 # indentation management
547 self.autoindent = False
549 self.autoindent = False
548 self.indent_current_nsp = 0
550 self.indent_current_nsp = 0
549
551
550 # Make some aliases automatically
552 # Make some aliases automatically
551 # Prepare list of shell aliases to auto-define
553 # Prepare list of shell aliases to auto-define
552 if os.name == 'posix':
554 if os.name == 'posix':
553 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
555 auto_alias = ('mkdir mkdir', 'rmdir rmdir',
554 'mv mv -i','rm rm -i','cp cp -i',
556 'mv mv -i','rm rm -i','cp cp -i',
555 'cat cat','less less','clear clear',
557 'cat cat','less less','clear clear',
556 # a better ls
558 # a better ls
557 'ls ls -F',
559 'ls ls -F',
558 # long ls
560 # long ls
559 'll ls -lF')
561 'll ls -lF')
560 # Extra ls aliases with color, which need special treatment on BSD
562 # Extra ls aliases with color, which need special treatment on BSD
561 # variants
563 # variants
562 ls_extra = ( # color ls
564 ls_extra = ( # color ls
563 'lc ls -F -o --color',
565 'lc ls -F -o --color',
564 # ls normal files only
566 # ls normal files only
565 'lf ls -F -o --color %l | grep ^-',
567 'lf ls -F -o --color %l | grep ^-',
566 # ls symbolic links
568 # ls symbolic links
567 'lk ls -F -o --color %l | grep ^l',
569 'lk ls -F -o --color %l | grep ^l',
568 # directories or links to directories,
570 # directories or links to directories,
569 'ldir ls -F -o --color %l | grep /$',
571 'ldir ls -F -o --color %l | grep /$',
570 # things which are executable
572 # things which are executable
571 'lx ls -F -o --color %l | grep ^-..x',
573 'lx ls -F -o --color %l | grep ^-..x',
572 )
574 )
573 # The BSDs don't ship GNU ls, so they don't understand the
575 # The BSDs don't ship GNU ls, so they don't understand the
574 # --color switch out of the box
576 # --color switch out of the box
575 if 'bsd' in sys.platform:
577 if 'bsd' in sys.platform:
576 ls_extra = ( # ls normal files only
578 ls_extra = ( # ls normal files only
577 'lf ls -lF | grep ^-',
579 'lf ls -lF | grep ^-',
578 # ls symbolic links
580 # ls symbolic links
579 'lk ls -lF | grep ^l',
581 'lk ls -lF | grep ^l',
580 # directories or links to directories,
582 # directories or links to directories,
581 'ldir ls -lF | grep /$',
583 'ldir ls -lF | grep /$',
582 # things which are executable
584 # things which are executable
583 'lx ls -lF | grep ^-..x',
585 'lx ls -lF | grep ^-..x',
584 )
586 )
585 auto_alias = auto_alias + ls_extra
587 auto_alias = auto_alias + ls_extra
586 elif os.name in ['nt','dos']:
588 elif os.name in ['nt','dos']:
587 auto_alias = ('ls dir /on',
589 auto_alias = ('ls dir /on',
588 'ddir dir /ad /on', 'ldir dir /ad /on',
590 'ddir dir /ad /on', 'ldir dir /ad /on',
589 'mkdir mkdir','rmdir rmdir','echo echo',
591 'mkdir mkdir','rmdir rmdir','echo echo',
590 'ren ren','cls cls','copy copy')
592 'ren ren','cls cls','copy copy')
591 else:
593 else:
592 auto_alias = ()
594 auto_alias = ()
593 self.auto_alias = [s.split(None,1) for s in auto_alias]
595 self.auto_alias = [s.split(None,1) for s in auto_alias]
594
596
595
597
596 # Produce a public API instance
598 # Produce a public API instance
597 self.api = IPython.ipapi.IPApi(self)
599 self.api = IPython.ipapi.IPApi(self)
598
600
599 # Call the actual (public) initializer
601 # Call the actual (public) initializer
600 self.init_auto_alias()
602 self.init_auto_alias()
601
603
602 # track which builtins we add, so we can clean up later
604 # track which builtins we add, so we can clean up later
603 self.builtins_added = {}
605 self.builtins_added = {}
604 # This method will add the necessary builtins for operation, but
606 # This method will add the necessary builtins for operation, but
605 # tracking what it did via the builtins_added dict.
607 # tracking what it did via the builtins_added dict.
606
608
607 #TODO: remove this, redundant
609 #TODO: remove this, redundant
608 self.add_builtins()
610 self.add_builtins()
609
611
610
612
611
613
612
614
613 # end __init__
615 # end __init__
614
616
615 def var_expand(self,cmd,depth=0):
617 def var_expand(self,cmd,depth=0):
616 """Expand python variables in a string.
618 """Expand python variables in a string.
617
619
618 The depth argument indicates how many frames above the caller should
620 The depth argument indicates how many frames above the caller should
619 be walked to look for the local namespace where to expand variables.
621 be walked to look for the local namespace where to expand variables.
620
622
621 The global namespace for expansion is always the user's interactive
623 The global namespace for expansion is always the user's interactive
622 namespace.
624 namespace.
623 """
625 """
624
626
625 return str(ItplNS(cmd,
627 return str(ItplNS(cmd,
626 self.user_ns, # globals
628 self.user_ns, # globals
627 # Skip our own frame in searching for locals:
629 # Skip our own frame in searching for locals:
628 sys._getframe(depth+1).f_locals # locals
630 sys._getframe(depth+1).f_locals # locals
629 ))
631 ))
630
632
631 def pre_config_initialization(self):
633 def pre_config_initialization(self):
632 """Pre-configuration init method
634 """Pre-configuration init method
633
635
634 This is called before the configuration files are processed to
636 This is called before the configuration files are processed to
635 prepare the services the config files might need.
637 prepare the services the config files might need.
636
638
637 self.rc already has reasonable default values at this point.
639 self.rc already has reasonable default values at this point.
638 """
640 """
639 rc = self.rc
641 rc = self.rc
640 try:
642 try:
641 self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db")
643 self.db = pickleshare.PickleShareDB(rc.ipythondir + "/db")
642 except exceptions.UnicodeDecodeError:
644 except exceptions.UnicodeDecodeError:
643 print "Your ipythondir can't be decoded to unicode!"
645 print "Your ipythondir can't be decoded to unicode!"
644 print "Please set HOME environment variable to something that"
646 print "Please set HOME environment variable to something that"
645 print r"only has ASCII characters, e.g. c:\home"
647 print r"only has ASCII characters, e.g. c:\home"
646 print "Now it is",rc.ipythondir
648 print "Now it is",rc.ipythondir
647 sys.exit()
649 sys.exit()
648 self.shadowhist = IPython.history.ShadowHist(self.db)
650 self.shadowhist = IPython.history.ShadowHist(self.db)
649
651
650
652
651 def post_config_initialization(self):
653 def post_config_initialization(self):
652 """Post configuration init method
654 """Post configuration init method
653
655
654 This is called after the configuration files have been processed to
656 This is called after the configuration files have been processed to
655 'finalize' the initialization."""
657 'finalize' the initialization."""
656
658
657 rc = self.rc
659 rc = self.rc
658
660
659 # Object inspector
661 # Object inspector
660 self.inspector = OInspect.Inspector(OInspect.InspectColors,
662 self.inspector = OInspect.Inspector(OInspect.InspectColors,
661 PyColorize.ANSICodeColors,
663 PyColorize.ANSICodeColors,
662 'NoColor',
664 'NoColor',
663 rc.object_info_string_level)
665 rc.object_info_string_level)
664
666
665 self.rl_next_input = None
667 self.rl_next_input = None
666 self.rl_do_indent = False
668 self.rl_do_indent = False
667 # Load readline proper
669 # Load readline proper
668 if rc.readline:
670 if rc.readline:
669 self.init_readline()
671 self.init_readline()
670
672
671
673
672 # local shortcut, this is used a LOT
674 # local shortcut, this is used a LOT
673 self.log = self.logger.log
675 self.log = self.logger.log
674
676
675 # Initialize cache, set in/out prompts and printing system
677 # Initialize cache, set in/out prompts and printing system
676 self.outputcache = CachedOutput(self,
678 self.outputcache = CachedOutput(self,
677 rc.cache_size,
679 rc.cache_size,
678 rc.pprint,
680 rc.pprint,
679 input_sep = rc.separate_in,
681 input_sep = rc.separate_in,
680 output_sep = rc.separate_out,
682 output_sep = rc.separate_out,
681 output_sep2 = rc.separate_out2,
683 output_sep2 = rc.separate_out2,
682 ps1 = rc.prompt_in1,
684 ps1 = rc.prompt_in1,
683 ps2 = rc.prompt_in2,
685 ps2 = rc.prompt_in2,
684 ps_out = rc.prompt_out,
686 ps_out = rc.prompt_out,
685 pad_left = rc.prompts_pad_left)
687 pad_left = rc.prompts_pad_left)
686
688
687 # user may have over-ridden the default print hook:
689 # user may have over-ridden the default print hook:
688 try:
690 try:
689 self.outputcache.__class__.display = self.hooks.display
691 self.outputcache.__class__.display = self.hooks.display
690 except AttributeError:
692 except AttributeError:
691 pass
693 pass
692
694
693 # I don't like assigning globally to sys, because it means when
695 # I don't like assigning globally to sys, because it means when
694 # embedding instances, each embedded instance overrides the previous
696 # embedding instances, each embedded instance overrides the previous
695 # choice. But sys.displayhook seems to be called internally by exec,
697 # choice. But sys.displayhook seems to be called internally by exec,
696 # so I don't see a way around it. We first save the original and then
698 # so I don't see a way around it. We first save the original and then
697 # overwrite it.
699 # overwrite it.
698 self.sys_displayhook = sys.displayhook
700 self.sys_displayhook = sys.displayhook
699 sys.displayhook = self.outputcache
701 sys.displayhook = self.outputcache
700
702
701 # Do a proper resetting of doctest, including the necessary displayhook
703 # Do a proper resetting of doctest, including the necessary displayhook
702 # monkeypatching
704 # monkeypatching
703 doctest_reload()
705 doctest_reload()
704
706
705 # Set user colors (don't do it in the constructor above so that it
707 # Set user colors (don't do it in the constructor above so that it
706 # doesn't crash if colors option is invalid)
708 # doesn't crash if colors option is invalid)
707 self.magic_colors(rc.colors)
709 self.magic_colors(rc.colors)
708
710
709 # Set calling of pdb on exceptions
711 # Set calling of pdb on exceptions
710 self.call_pdb = rc.pdb
712 self.call_pdb = rc.pdb
711
713
712 # Load user aliases
714 # Load user aliases
713 for alias in rc.alias:
715 for alias in rc.alias:
714 self.magic_alias(alias)
716 self.magic_alias(alias)
715
717
716 self.hooks.late_startup_hook()
718 self.hooks.late_startup_hook()
717
719
718 for cmd in self.rc.autoexec:
720 for cmd in self.rc.autoexec:
719 #print "autoexec>",cmd #dbg
721 #print "autoexec>",cmd #dbg
720 self.api.runlines(cmd)
722 self.api.runlines(cmd)
721
723
722 batchrun = False
724 batchrun = False
723 for batchfile in [path(arg) for arg in self.rc.args
725 for batchfile in [path(arg) for arg in self.rc.args
724 if arg.lower().endswith('.ipy')]:
726 if arg.lower().endswith('.ipy')]:
725 if not batchfile.isfile():
727 if not batchfile.isfile():
726 print "No such batch file:", batchfile
728 print "No such batch file:", batchfile
727 continue
729 continue
728 self.api.runlines(batchfile.text())
730 self.api.runlines(batchfile.text())
729 batchrun = True
731 batchrun = True
730 # without -i option, exit after running the batch file
732 # without -i option, exit after running the batch file
731 if batchrun and not self.rc.interact:
733 if batchrun and not self.rc.interact:
732 self.exit_now = True
734 self.exit_now = True
733
735
734 def add_builtins(self):
736 def add_builtins(self):
735 """Store ipython references into the builtin namespace.
737 """Store ipython references into the builtin namespace.
736
738
737 Some parts of ipython operate via builtins injected here, which hold a
739 Some parts of ipython operate via builtins injected here, which hold a
738 reference to IPython itself."""
740 reference to IPython itself."""
739
741
740 # TODO: deprecate all of these, they are unsafe
742 # TODO: deprecate all of these, they are unsafe
741 builtins_new = dict(__IPYTHON__ = self,
743 builtins_new = dict(__IPYTHON__ = self,
742 ip_set_hook = self.set_hook,
744 ip_set_hook = self.set_hook,
743 jobs = self.jobs,
745 jobs = self.jobs,
744 ipmagic = wrap_deprecated(self.ipmagic,'_ip.magic()'),
746 ipmagic = wrap_deprecated(self.ipmagic,'_ip.magic()'),
745 ipalias = wrap_deprecated(self.ipalias),
747 ipalias = wrap_deprecated(self.ipalias),
746 ipsystem = wrap_deprecated(self.ipsystem,'_ip.system()'),
748 ipsystem = wrap_deprecated(self.ipsystem,'_ip.system()'),
747 #_ip = self.api
749 #_ip = self.api
748 )
750 )
749 for biname,bival in builtins_new.items():
751 for biname,bival in builtins_new.items():
750 try:
752 try:
751 # store the orignal value so we can restore it
753 # store the orignal value so we can restore it
752 self.builtins_added[biname] = __builtin__.__dict__[biname]
754 self.builtins_added[biname] = __builtin__.__dict__[biname]
753 except KeyError:
755 except KeyError:
754 # or mark that it wasn't defined, and we'll just delete it at
756 # or mark that it wasn't defined, and we'll just delete it at
755 # cleanup
757 # cleanup
756 self.builtins_added[biname] = Undefined
758 self.builtins_added[biname] = Undefined
757 __builtin__.__dict__[biname] = bival
759 __builtin__.__dict__[biname] = bival
758
760
759 # Keep in the builtins a flag for when IPython is active. We set it
761 # Keep in the builtins a flag for when IPython is active. We set it
760 # with setdefault so that multiple nested IPythons don't clobber one
762 # with setdefault so that multiple nested IPythons don't clobber one
761 # another. Each will increase its value by one upon being activated,
763 # another. Each will increase its value by one upon being activated,
762 # which also gives us a way to determine the nesting level.
764 # which also gives us a way to determine the nesting level.
763 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
765 __builtin__.__dict__.setdefault('__IPYTHON__active',0)
764
766
765 def clean_builtins(self):
767 def clean_builtins(self):
766 """Remove any builtins which might have been added by add_builtins, or
768 """Remove any builtins which might have been added by add_builtins, or
767 restore overwritten ones to their previous values."""
769 restore overwritten ones to their previous values."""
768 for biname,bival in self.builtins_added.items():
770 for biname,bival in self.builtins_added.items():
769 if bival is Undefined:
771 if bival is Undefined:
770 del __builtin__.__dict__[biname]
772 del __builtin__.__dict__[biname]
771 else:
773 else:
772 __builtin__.__dict__[biname] = bival
774 __builtin__.__dict__[biname] = bival
773 self.builtins_added.clear()
775 self.builtins_added.clear()
774
776
775 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
777 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
776 """set_hook(name,hook) -> sets an internal IPython hook.
778 """set_hook(name,hook) -> sets an internal IPython hook.
777
779
778 IPython exposes some of its internal API as user-modifiable hooks. By
780 IPython exposes some of its internal API as user-modifiable hooks. By
779 adding your function to one of these hooks, you can modify IPython's
781 adding your function to one of these hooks, you can modify IPython's
780 behavior to call at runtime your own routines."""
782 behavior to call at runtime your own routines."""
781
783
782 # At some point in the future, this should validate the hook before it
784 # At some point in the future, this should validate the hook before it
783 # accepts it. Probably at least check that the hook takes the number
785 # accepts it. Probably at least check that the hook takes the number
784 # of args it's supposed to.
786 # of args it's supposed to.
785
787
786 f = new.instancemethod(hook,self,self.__class__)
788 f = new.instancemethod(hook,self,self.__class__)
787
789
788 # check if the hook is for strdispatcher first
790 # check if the hook is for strdispatcher first
789 if str_key is not None:
791 if str_key is not None:
790 sdp = self.strdispatchers.get(name, StrDispatch())
792 sdp = self.strdispatchers.get(name, StrDispatch())
791 sdp.add_s(str_key, f, priority )
793 sdp.add_s(str_key, f, priority )
792 self.strdispatchers[name] = sdp
794 self.strdispatchers[name] = sdp
793 return
795 return
794 if re_key is not None:
796 if re_key is not None:
795 sdp = self.strdispatchers.get(name, StrDispatch())
797 sdp = self.strdispatchers.get(name, StrDispatch())
796 sdp.add_re(re.compile(re_key), f, priority )
798 sdp.add_re(re.compile(re_key), f, priority )
797 self.strdispatchers[name] = sdp
799 self.strdispatchers[name] = sdp
798 return
800 return
799
801
800 dp = getattr(self.hooks, name, None)
802 dp = getattr(self.hooks, name, None)
801 if name not in IPython.hooks.__all__:
803 if name not in IPython.hooks.__all__:
802 print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ )
804 print "Warning! Hook '%s' is not one of %s" % (name, IPython.hooks.__all__ )
803 if not dp:
805 if not dp:
804 dp = IPython.hooks.CommandChainDispatcher()
806 dp = IPython.hooks.CommandChainDispatcher()
805
807
806 try:
808 try:
807 dp.add(f,priority)
809 dp.add(f,priority)
808 except AttributeError:
810 except AttributeError:
809 # it was not commandchain, plain old func - replace
811 # it was not commandchain, plain old func - replace
810 dp = f
812 dp = f
811
813
812 setattr(self.hooks,name, dp)
814 setattr(self.hooks,name, dp)
813
815
814
816
815 #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
817 #setattr(self.hooks,name,new.instancemethod(hook,self,self.__class__))
816
818
817 def set_crash_handler(self,crashHandler):
819 def set_crash_handler(self,crashHandler):
818 """Set the IPython crash handler.
820 """Set the IPython crash handler.
819
821
820 This must be a callable with a signature suitable for use as
822 This must be a callable with a signature suitable for use as
821 sys.excepthook."""
823 sys.excepthook."""
822
824
823 # Install the given crash handler as the Python exception hook
825 # Install the given crash handler as the Python exception hook
824 sys.excepthook = crashHandler
826 sys.excepthook = crashHandler
825
827
826 # The instance will store a pointer to this, so that runtime code
828 # The instance will store a pointer to this, so that runtime code
827 # (such as magics) can access it. This is because during the
829 # (such as magics) can access it. This is because during the
828 # read-eval loop, it gets temporarily overwritten (to deal with GUI
830 # read-eval loop, it gets temporarily overwritten (to deal with GUI
829 # frameworks).
831 # frameworks).
830 self.sys_excepthook = sys.excepthook
832 self.sys_excepthook = sys.excepthook
831
833
832
834
833 def set_custom_exc(self,exc_tuple,handler):
835 def set_custom_exc(self,exc_tuple,handler):
834 """set_custom_exc(exc_tuple,handler)
836 """set_custom_exc(exc_tuple,handler)
835
837
836 Set a custom exception handler, which will be called if any of the
838 Set a custom exception handler, which will be called if any of the
837 exceptions in exc_tuple occur in the mainloop (specifically, in the
839 exceptions in exc_tuple occur in the mainloop (specifically, in the
838 runcode() method.
840 runcode() method.
839
841
840 Inputs:
842 Inputs:
841
843
842 - exc_tuple: a *tuple* of valid exceptions to call the defined
844 - exc_tuple: a *tuple* of valid exceptions to call the defined
843 handler for. It is very important that you use a tuple, and NOT A
845 handler for. It is very important that you use a tuple, and NOT A
844 LIST here, because of the way Python's except statement works. If
846 LIST here, because of the way Python's except statement works. If
845 you only want to trap a single exception, use a singleton tuple:
847 you only want to trap a single exception, use a singleton tuple:
846
848
847 exc_tuple == (MyCustomException,)
849 exc_tuple == (MyCustomException,)
848
850
849 - handler: this must be defined as a function with the following
851 - handler: this must be defined as a function with the following
850 basic interface: def my_handler(self,etype,value,tb).
852 basic interface: def my_handler(self,etype,value,tb).
851
853
852 This will be made into an instance method (via new.instancemethod)
854 This will be made into an instance method (via new.instancemethod)
853 of IPython itself, and it will be called if any of the exceptions
855 of IPython itself, and it will be called if any of the exceptions
854 listed in the exc_tuple are caught. If the handler is None, an
856 listed in the exc_tuple are caught. If the handler is None, an
855 internal basic one is used, which just prints basic info.
857 internal basic one is used, which just prints basic info.
856
858
857 WARNING: by putting in your own exception handler into IPython's main
859 WARNING: by putting in your own exception handler into IPython's main
858 execution loop, you run a very good chance of nasty crashes. This
860 execution loop, you run a very good chance of nasty crashes. This
859 facility should only be used if you really know what you are doing."""
861 facility should only be used if you really know what you are doing."""
860
862
861 assert type(exc_tuple)==type(()) , \
863 assert type(exc_tuple)==type(()) , \
862 "The custom exceptions must be given AS A TUPLE."
864 "The custom exceptions must be given AS A TUPLE."
863
865
864 def dummy_handler(self,etype,value,tb):
866 def dummy_handler(self,etype,value,tb):
865 print '*** Simple custom exception handler ***'
867 print '*** Simple custom exception handler ***'
866 print 'Exception type :',etype
868 print 'Exception type :',etype
867 print 'Exception value:',value
869 print 'Exception value:',value
868 print 'Traceback :',tb
870 print 'Traceback :',tb
869 print 'Source code :','\n'.join(self.buffer)
871 print 'Source code :','\n'.join(self.buffer)
870
872
871 if handler is None: handler = dummy_handler
873 if handler is None: handler = dummy_handler
872
874
873 self.CustomTB = new.instancemethod(handler,self,self.__class__)
875 self.CustomTB = new.instancemethod(handler,self,self.__class__)
874 self.custom_exceptions = exc_tuple
876 self.custom_exceptions = exc_tuple
875
877
876 def set_custom_completer(self,completer,pos=0):
878 def set_custom_completer(self,completer,pos=0):
877 """set_custom_completer(completer,pos=0)
879 """set_custom_completer(completer,pos=0)
878
880
879 Adds a new custom completer function.
881 Adds a new custom completer function.
880
882
881 The position argument (defaults to 0) is the index in the completers
883 The position argument (defaults to 0) is the index in the completers
882 list where you want the completer to be inserted."""
884 list where you want the completer to be inserted."""
883
885
884 newcomp = new.instancemethod(completer,self.Completer,
886 newcomp = new.instancemethod(completer,self.Completer,
885 self.Completer.__class__)
887 self.Completer.__class__)
886 self.Completer.matchers.insert(pos,newcomp)
888 self.Completer.matchers.insert(pos,newcomp)
887
889
888 def set_completer(self):
890 def set_completer(self):
889 """reset readline's completer to be our own."""
891 """reset readline's completer to be our own."""
890 self.readline.set_completer(self.Completer.complete)
892 self.readline.set_completer(self.Completer.complete)
891
893
892 def _get_call_pdb(self):
894 def _get_call_pdb(self):
893 return self._call_pdb
895 return self._call_pdb
894
896
895 def _set_call_pdb(self,val):
897 def _set_call_pdb(self,val):
896
898
897 if val not in (0,1,False,True):
899 if val not in (0,1,False,True):
898 raise ValueError,'new call_pdb value must be boolean'
900 raise ValueError,'new call_pdb value must be boolean'
899
901
900 # store value in instance
902 # store value in instance
901 self._call_pdb = val
903 self._call_pdb = val
902
904
903 # notify the actual exception handlers
905 # notify the actual exception handlers
904 self.InteractiveTB.call_pdb = val
906 self.InteractiveTB.call_pdb = val
905 if self.isthreaded:
907 if self.isthreaded:
906 try:
908 try:
907 self.sys_excepthook.call_pdb = val
909 self.sys_excepthook.call_pdb = val
908 except:
910 except:
909 warn('Failed to activate pdb for threaded exception handler')
911 warn('Failed to activate pdb for threaded exception handler')
910
912
911 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
913 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
912 'Control auto-activation of pdb at exceptions')
914 'Control auto-activation of pdb at exceptions')
913
915
914
916
915 # These special functions get installed in the builtin namespace, to
917 # These special functions get installed in the builtin namespace, to
916 # provide programmatic (pure python) access to magics, aliases and system
918 # provide programmatic (pure python) access to magics, aliases and system
917 # calls. This is important for logging, user scripting, and more.
919 # calls. This is important for logging, user scripting, and more.
918
920
919 # We are basically exposing, via normal python functions, the three
921 # We are basically exposing, via normal python functions, the three
920 # mechanisms in which ipython offers special call modes (magics for
922 # mechanisms in which ipython offers special call modes (magics for
921 # internal control, aliases for direct system access via pre-selected
923 # internal control, aliases for direct system access via pre-selected
922 # names, and !cmd for calling arbitrary system commands).
924 # names, and !cmd for calling arbitrary system commands).
923
925
924 def ipmagic(self,arg_s):
926 def ipmagic(self,arg_s):
925 """Call a magic function by name.
927 """Call a magic function by name.
926
928
927 Input: a string containing the name of the magic function to call and any
929 Input: a string containing the name of the magic function to call and any
928 additional arguments to be passed to the magic.
930 additional arguments to be passed to the magic.
929
931
930 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
932 ipmagic('name -opt foo bar') is equivalent to typing at the ipython
931 prompt:
933 prompt:
932
934
933 In[1]: %name -opt foo bar
935 In[1]: %name -opt foo bar
934
936
935 To call a magic without arguments, simply use ipmagic('name').
937 To call a magic without arguments, simply use ipmagic('name').
936
938
937 This provides a proper Python function to call IPython's magics in any
939 This provides a proper Python function to call IPython's magics in any
938 valid Python code you can type at the interpreter, including loops and
940 valid Python code you can type at the interpreter, including loops and
939 compound statements. It is added by IPython to the Python builtin
941 compound statements. It is added by IPython to the Python builtin
940 namespace upon initialization."""
942 namespace upon initialization."""
941
943
942 args = arg_s.split(' ',1)
944 args = arg_s.split(' ',1)
943 magic_name = args[0]
945 magic_name = args[0]
944 magic_name = magic_name.lstrip(self.ESC_MAGIC)
946 magic_name = magic_name.lstrip(self.ESC_MAGIC)
945
947
946 try:
948 try:
947 magic_args = args[1]
949 magic_args = args[1]
948 except IndexError:
950 except IndexError:
949 magic_args = ''
951 magic_args = ''
950 fn = getattr(self,'magic_'+magic_name,None)
952 fn = getattr(self,'magic_'+magic_name,None)
951 if fn is None:
953 if fn is None:
952 error("Magic function `%s` not found." % magic_name)
954 error("Magic function `%s` not found." % magic_name)
953 else:
955 else:
954 magic_args = self.var_expand(magic_args,1)
956 magic_args = self.var_expand(magic_args,1)
955 return fn(magic_args)
957 return fn(magic_args)
956
958
957 def ipalias(self,arg_s):
959 def ipalias(self,arg_s):
958 """Call an alias by name.
960 """Call an alias by name.
959
961
960 Input: a string containing the name of the alias to call and any
962 Input: a string containing the name of the alias to call and any
961 additional arguments to be passed to the magic.
963 additional arguments to be passed to the magic.
962
964
963 ipalias('name -opt foo bar') is equivalent to typing at the ipython
965 ipalias('name -opt foo bar') is equivalent to typing at the ipython
964 prompt:
966 prompt:
965
967
966 In[1]: name -opt foo bar
968 In[1]: name -opt foo bar
967
969
968 To call an alias without arguments, simply use ipalias('name').
970 To call an alias without arguments, simply use ipalias('name').
969
971
970 This provides a proper Python function to call IPython's aliases in any
972 This provides a proper Python function to call IPython's aliases in any
971 valid Python code you can type at the interpreter, including loops and
973 valid Python code you can type at the interpreter, including loops and
972 compound statements. It is added by IPython to the Python builtin
974 compound statements. It is added by IPython to the Python builtin
973 namespace upon initialization."""
975 namespace upon initialization."""
974
976
975 args = arg_s.split(' ',1)
977 args = arg_s.split(' ',1)
976 alias_name = args[0]
978 alias_name = args[0]
977 try:
979 try:
978 alias_args = args[1]
980 alias_args = args[1]
979 except IndexError:
981 except IndexError:
980 alias_args = ''
982 alias_args = ''
981 if alias_name in self.alias_table:
983 if alias_name in self.alias_table:
982 self.call_alias(alias_name,alias_args)
984 self.call_alias(alias_name,alias_args)
983 else:
985 else:
984 error("Alias `%s` not found." % alias_name)
986 error("Alias `%s` not found." % alias_name)
985
987
986 def ipsystem(self,arg_s):
988 def ipsystem(self,arg_s):
987 """Make a system call, using IPython."""
989 """Make a system call, using IPython."""
988
990
989 self.system(arg_s)
991 self.system(arg_s)
990
992
991 def complete(self,text):
993 def complete(self,text):
992 """Return a sorted list of all possible completions on text.
994 """Return a sorted list of all possible completions on text.
993
995
994 Inputs:
996 Inputs:
995
997
996 - text: a string of text to be completed on.
998 - text: a string of text to be completed on.
997
999
998 This is a wrapper around the completion mechanism, similar to what
1000 This is a wrapper around the completion mechanism, similar to what
999 readline does at the command line when the TAB key is hit. By
1001 readline does at the command line when the TAB key is hit. By
1000 exposing it as a method, it can be used by other non-readline
1002 exposing it as a method, it can be used by other non-readline
1001 environments (such as GUIs) for text completion.
1003 environments (such as GUIs) for text completion.
1002
1004
1003 Simple usage example:
1005 Simple usage example:
1004
1006
1005 In [1]: x = 'hello'
1007 In [1]: x = 'hello'
1006
1008
1007 In [2]: __IP.complete('x.l')
1009 In [2]: __IP.complete('x.l')
1008 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
1010 Out[2]: ['x.ljust', 'x.lower', 'x.lstrip']"""
1009
1011
1010 complete = self.Completer.complete
1012 complete = self.Completer.complete
1011 state = 0
1013 state = 0
1012 # use a dict so we get unique keys, since ipyhton's multiple
1014 # use a dict so we get unique keys, since ipyhton's multiple
1013 # completers can return duplicates. When we make 2.4 a requirement,
1015 # completers can return duplicates. When we make 2.4 a requirement,
1014 # start using sets instead, which are faster.
1016 # start using sets instead, which are faster.
1015 comps = {}
1017 comps = {}
1016 while True:
1018 while True:
1017 newcomp = complete(text,state,line_buffer=text)
1019 newcomp = complete(text,state,line_buffer=text)
1018 if newcomp is None:
1020 if newcomp is None:
1019 break
1021 break
1020 comps[newcomp] = 1
1022 comps[newcomp] = 1
1021 state += 1
1023 state += 1
1022 outcomps = comps.keys()
1024 outcomps = comps.keys()
1023 outcomps.sort()
1025 outcomps.sort()
1024 return outcomps
1026 return outcomps
1025
1027
1026 def set_completer_frame(self, frame=None):
1028 def set_completer_frame(self, frame=None):
1027 if frame:
1029 if frame:
1028 self.Completer.namespace = frame.f_locals
1030 self.Completer.namespace = frame.f_locals
1029 self.Completer.global_namespace = frame.f_globals
1031 self.Completer.global_namespace = frame.f_globals
1030 else:
1032 else:
1031 self.Completer.namespace = self.user_ns
1033 self.Completer.namespace = self.user_ns
1032 self.Completer.global_namespace = self.user_global_ns
1034 self.Completer.global_namespace = self.user_global_ns
1033
1035
1034 def init_auto_alias(self):
1036 def init_auto_alias(self):
1035 """Define some aliases automatically.
1037 """Define some aliases automatically.
1036
1038
1037 These are ALL parameter-less aliases"""
1039 These are ALL parameter-less aliases"""
1038
1040
1039 for alias,cmd in self.auto_alias:
1041 for alias,cmd in self.auto_alias:
1040 self.getapi().defalias(alias,cmd)
1042 self.getapi().defalias(alias,cmd)
1041
1043
1042
1044
1043 def alias_table_validate(self,verbose=0):
1045 def alias_table_validate(self,verbose=0):
1044 """Update information about the alias table.
1046 """Update information about the alias table.
1045
1047
1046 In particular, make sure no Python keywords/builtins are in it."""
1048 In particular, make sure no Python keywords/builtins are in it."""
1047
1049
1048 no_alias = self.no_alias
1050 no_alias = self.no_alias
1049 for k in self.alias_table.keys():
1051 for k in self.alias_table.keys():
1050 if k in no_alias:
1052 if k in no_alias:
1051 del self.alias_table[k]
1053 del self.alias_table[k]
1052 if verbose:
1054 if verbose:
1053 print ("Deleting alias <%s>, it's a Python "
1055 print ("Deleting alias <%s>, it's a Python "
1054 "keyword or builtin." % k)
1056 "keyword or builtin." % k)
1055
1057
1056 def set_autoindent(self,value=None):
1058 def set_autoindent(self,value=None):
1057 """Set the autoindent flag, checking for readline support.
1059 """Set the autoindent flag, checking for readline support.
1058
1060
1059 If called with no arguments, it acts as a toggle."""
1061 If called with no arguments, it acts as a toggle."""
1060
1062
1061 if not self.has_readline:
1063 if not self.has_readline:
1062 if os.name == 'posix':
1064 if os.name == 'posix':
1063 warn("The auto-indent feature requires the readline library")
1065 warn("The auto-indent feature requires the readline library")
1064 self.autoindent = 0
1066 self.autoindent = 0
1065 return
1067 return
1066 if value is None:
1068 if value is None:
1067 self.autoindent = not self.autoindent
1069 self.autoindent = not self.autoindent
1068 else:
1070 else:
1069 self.autoindent = value
1071 self.autoindent = value
1070
1072
1071 def rc_set_toggle(self,rc_field,value=None):
1073 def rc_set_toggle(self,rc_field,value=None):
1072 """Set or toggle a field in IPython's rc config. structure.
1074 """Set or toggle a field in IPython's rc config. structure.
1073
1075
1074 If called with no arguments, it acts as a toggle.
1076 If called with no arguments, it acts as a toggle.
1075
1077
1076 If called with a non-existent field, the resulting AttributeError
1078 If called with a non-existent field, the resulting AttributeError
1077 exception will propagate out."""
1079 exception will propagate out."""
1078
1080
1079 rc_val = getattr(self.rc,rc_field)
1081 rc_val = getattr(self.rc,rc_field)
1080 if value is None:
1082 if value is None:
1081 value = not rc_val
1083 value = not rc_val
1082 setattr(self.rc,rc_field,value)
1084 setattr(self.rc,rc_field,value)
1083
1085
1084 def user_setup(self,ipythondir,rc_suffix,mode='install'):
1086 def user_setup(self,ipythondir,rc_suffix,mode='install'):
1085 """Install the user configuration directory.
1087 """Install the user configuration directory.
1086
1088
1087 Can be called when running for the first time or to upgrade the user's
1089 Can be called when running for the first time or to upgrade the user's
1088 .ipython/ directory with the mode parameter. Valid modes are 'install'
1090 .ipython/ directory with the mode parameter. Valid modes are 'install'
1089 and 'upgrade'."""
1091 and 'upgrade'."""
1090
1092
1091 def wait():
1093 def wait():
1092 try:
1094 try:
1093 raw_input("Please press <RETURN> to start IPython.")
1095 raw_input("Please press <RETURN> to start IPython.")
1094 except EOFError:
1096 except EOFError:
1095 print >> Term.cout
1097 print >> Term.cout
1096 print '*'*70
1098 print '*'*70
1097
1099
1098 cwd = os.getcwd() # remember where we started
1100 cwd = os.getcwd() # remember where we started
1099 glb = glob.glob
1101 glb = glob.glob
1100 print '*'*70
1102 print '*'*70
1101 if mode == 'install':
1103 if mode == 'install':
1102 print \
1104 print \
1103 """Welcome to IPython. I will try to create a personal configuration directory
1105 """Welcome to IPython. I will try to create a personal configuration directory
1104 where you can customize many aspects of IPython's functionality in:\n"""
1106 where you can customize many aspects of IPython's functionality in:\n"""
1105 else:
1107 else:
1106 print 'I am going to upgrade your configuration in:'
1108 print 'I am going to upgrade your configuration in:'
1107
1109
1108 print ipythondir
1110 print ipythondir
1109
1111
1110 rcdirend = os.path.join('IPython','UserConfig')
1112 rcdirend = os.path.join('IPython','UserConfig')
1111 cfg = lambda d: os.path.join(d,rcdirend)
1113 cfg = lambda d: os.path.join(d,rcdirend)
1112 try:
1114 try:
1113 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
1115 rcdir = filter(os.path.isdir,map(cfg,sys.path))[0]
1114 print "Initializing from configuration",rcdir
1116 print "Initializing from configuration",rcdir
1115 except IndexError:
1117 except IndexError:
1116 warning = """
1118 warning = """
1117 Installation error. IPython's directory was not found.
1119 Installation error. IPython's directory was not found.
1118
1120
1119 Check the following:
1121 Check the following:
1120
1122
1121 The ipython/IPython directory should be in a directory belonging to your
1123 The ipython/IPython directory should be in a directory belonging to your
1122 PYTHONPATH environment variable (that is, it should be in a directory
1124 PYTHONPATH environment variable (that is, it should be in a directory
1123 belonging to sys.path). You can copy it explicitly there or just link to it.
1125 belonging to sys.path). You can copy it explicitly there or just link to it.
1124
1126
1125 IPython will create a minimal default configuration for you.
1127 IPython will create a minimal default configuration for you.
1126
1128
1127 """
1129 """
1128 warn(warning)
1130 warn(warning)
1129 wait()
1131 wait()
1130
1132
1131 if sys.platform =='win32':
1133 if sys.platform =='win32':
1132 inif = 'ipythonrc.ini'
1134 inif = 'ipythonrc.ini'
1133 else:
1135 else:
1134 inif = 'ipythonrc'
1136 inif = 'ipythonrc'
1135 minimal_setup = {'ipy_user_conf.py' : 'import ipy_defaults', inif : '# intentionally left blank' }
1137 minimal_setup = {'ipy_user_conf.py' : 'import ipy_defaults', inif : '# intentionally left blank' }
1136 os.makedirs(ipythondir, mode = 0777)
1138 os.makedirs(ipythondir, mode = 0777)
1137 for f, cont in minimal_setup.items():
1139 for f, cont in minimal_setup.items():
1138 open(ipythondir + '/' + f,'w').write(cont)
1140 open(ipythondir + '/' + f,'w').write(cont)
1139
1141
1140 return
1142 return
1141
1143
1142 if mode == 'install':
1144 if mode == 'install':
1143 try:
1145 try:
1144 shutil.copytree(rcdir,ipythondir)
1146 shutil.copytree(rcdir,ipythondir)
1145 os.chdir(ipythondir)
1147 os.chdir(ipythondir)
1146 rc_files = glb("ipythonrc*")
1148 rc_files = glb("ipythonrc*")
1147 for rc_file in rc_files:
1149 for rc_file in rc_files:
1148 os.rename(rc_file,rc_file+rc_suffix)
1150 os.rename(rc_file,rc_file+rc_suffix)
1149 except:
1151 except:
1150 warning = """
1152 warning = """
1151
1153
1152 There was a problem with the installation:
1154 There was a problem with the installation:
1153 %s
1155 %s
1154 Try to correct it or contact the developers if you think it's a bug.
1156 Try to correct it or contact the developers if you think it's a bug.
1155 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1157 IPython will proceed with builtin defaults.""" % sys.exc_info()[1]
1156 warn(warning)
1158 warn(warning)
1157 wait()
1159 wait()
1158 return
1160 return
1159
1161
1160 elif mode == 'upgrade':
1162 elif mode == 'upgrade':
1161 try:
1163 try:
1162 os.chdir(ipythondir)
1164 os.chdir(ipythondir)
1163 except:
1165 except:
1164 print """
1166 print """
1165 Can not upgrade: changing to directory %s failed. Details:
1167 Can not upgrade: changing to directory %s failed. Details:
1166 %s
1168 %s
1167 """ % (ipythondir,sys.exc_info()[1])
1169 """ % (ipythondir,sys.exc_info()[1])
1168 wait()
1170 wait()
1169 return
1171 return
1170 else:
1172 else:
1171 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1173 sources = glb(os.path.join(rcdir,'[A-Za-z]*'))
1172 for new_full_path in sources:
1174 for new_full_path in sources:
1173 new_filename = os.path.basename(new_full_path)
1175 new_filename = os.path.basename(new_full_path)
1174 if new_filename.startswith('ipythonrc'):
1176 if new_filename.startswith('ipythonrc'):
1175 new_filename = new_filename + rc_suffix
1177 new_filename = new_filename + rc_suffix
1176 # The config directory should only contain files, skip any
1178 # The config directory should only contain files, skip any
1177 # directories which may be there (like CVS)
1179 # directories which may be there (like CVS)
1178 if os.path.isdir(new_full_path):
1180 if os.path.isdir(new_full_path):
1179 continue
1181 continue
1180 if os.path.exists(new_filename):
1182 if os.path.exists(new_filename):
1181 old_file = new_filename+'.old'
1183 old_file = new_filename+'.old'
1182 if os.path.exists(old_file):
1184 if os.path.exists(old_file):
1183 os.remove(old_file)
1185 os.remove(old_file)
1184 os.rename(new_filename,old_file)
1186 os.rename(new_filename,old_file)
1185 shutil.copy(new_full_path,new_filename)
1187 shutil.copy(new_full_path,new_filename)
1186 else:
1188 else:
1187 raise ValueError,'unrecognized mode for install:',`mode`
1189 raise ValueError,'unrecognized mode for install:',`mode`
1188
1190
1189 # Fix line-endings to those native to each platform in the config
1191 # Fix line-endings to those native to each platform in the config
1190 # directory.
1192 # directory.
1191 try:
1193 try:
1192 os.chdir(ipythondir)
1194 os.chdir(ipythondir)
1193 except:
1195 except:
1194 print """
1196 print """
1195 Problem: changing to directory %s failed.
1197 Problem: changing to directory %s failed.
1196 Details:
1198 Details:
1197 %s
1199 %s
1198
1200
1199 Some configuration files may have incorrect line endings. This should not
1201 Some configuration files may have incorrect line endings. This should not
1200 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1202 cause any problems during execution. """ % (ipythondir,sys.exc_info()[1])
1201 wait()
1203 wait()
1202 else:
1204 else:
1203 for fname in glb('ipythonrc*'):
1205 for fname in glb('ipythonrc*'):
1204 try:
1206 try:
1205 native_line_ends(fname,backup=0)
1207 native_line_ends(fname,backup=0)
1206 except IOError:
1208 except IOError:
1207 pass
1209 pass
1208
1210
1209 if mode == 'install':
1211 if mode == 'install':
1210 print """
1212 print """
1211 Successful installation!
1213 Successful installation!
1212
1214
1213 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1215 Please read the sections 'Initial Configuration' and 'Quick Tips' in the
1214 IPython manual (there are both HTML and PDF versions supplied with the
1216 IPython manual (there are both HTML and PDF versions supplied with the
1215 distribution) to make sure that your system environment is properly configured
1217 distribution) to make sure that your system environment is properly configured
1216 to take advantage of IPython's features.
1218 to take advantage of IPython's features.
1217
1219
1218 Important note: the configuration system has changed! The old system is
1220 Important note: the configuration system has changed! The old system is
1219 still in place, but its setting may be partly overridden by the settings in
1221 still in place, but its setting may be partly overridden by the settings in
1220 "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file
1222 "~/.ipython/ipy_user_conf.py" config file. Please take a look at the file
1221 if some of the new settings bother you.
1223 if some of the new settings bother you.
1222
1224
1223 """
1225 """
1224 else:
1226 else:
1225 print """
1227 print """
1226 Successful upgrade!
1228 Successful upgrade!
1227
1229
1228 All files in your directory:
1230 All files in your directory:
1229 %(ipythondir)s
1231 %(ipythondir)s
1230 which would have been overwritten by the upgrade were backed up with a .old
1232 which would have been overwritten by the upgrade were backed up with a .old
1231 extension. If you had made particular customizations in those files you may
1233 extension. If you had made particular customizations in those files you may
1232 want to merge them back into the new files.""" % locals()
1234 want to merge them back into the new files.""" % locals()
1233 wait()
1235 wait()
1234 os.chdir(cwd)
1236 os.chdir(cwd)
1235 # end user_setup()
1237 # end user_setup()
1236
1238
1237 def atexit_operations(self):
1239 def atexit_operations(self):
1238 """This will be executed at the time of exit.
1240 """This will be executed at the time of exit.
1239
1241
1240 Saving of persistent data should be performed here. """
1242 Saving of persistent data should be performed here. """
1241
1243
1242 #print '*** IPython exit cleanup ***' # dbg
1244 #print '*** IPython exit cleanup ***' # dbg
1243 # input history
1245 # input history
1244 self.savehist()
1246 self.savehist()
1245
1247
1246 # Cleanup all tempfiles left around
1248 # Cleanup all tempfiles left around
1247 for tfile in self.tempfiles:
1249 for tfile in self.tempfiles:
1248 try:
1250 try:
1249 os.unlink(tfile)
1251 os.unlink(tfile)
1250 except OSError:
1252 except OSError:
1251 pass
1253 pass
1252
1254
1253 self.hooks.shutdown_hook()
1255 self.hooks.shutdown_hook()
1254
1256
1255 def savehist(self):
1257 def savehist(self):
1256 """Save input history to a file (via readline library)."""
1258 """Save input history to a file (via readline library)."""
1257
1259
1258 if not self.has_readline:
1260 if not self.has_readline:
1259 return
1261 return
1260
1262
1261 try:
1263 try:
1262 self.readline.write_history_file(self.histfile)
1264 self.readline.write_history_file(self.histfile)
1263 except:
1265 except:
1264 print 'Unable to save IPython command history to file: ' + \
1266 print 'Unable to save IPython command history to file: ' + \
1265 `self.histfile`
1267 `self.histfile`
1266
1268
1267 def reloadhist(self):
1269 def reloadhist(self):
1268 """Reload the input history from disk file."""
1270 """Reload the input history from disk file."""
1269
1271
1270 if self.has_readline:
1272 if self.has_readline:
1271 try:
1273 try:
1272 self.readline.clear_history()
1274 self.readline.clear_history()
1273 self.readline.read_history_file(self.shell.histfile)
1275 self.readline.read_history_file(self.shell.histfile)
1274 except AttributeError:
1276 except AttributeError:
1275 pass
1277 pass
1276
1278
1277
1279
1278 def history_saving_wrapper(self, func):
1280 def history_saving_wrapper(self, func):
1279 """ Wrap func for readline history saving
1281 """ Wrap func for readline history saving
1280
1282
1281 Convert func into callable that saves & restores
1283 Convert func into callable that saves & restores
1282 history around the call """
1284 history around the call """
1283
1285
1284 if not self.has_readline:
1286 if not self.has_readline:
1285 return func
1287 return func
1286
1288
1287 def wrapper():
1289 def wrapper():
1288 self.savehist()
1290 self.savehist()
1289 try:
1291 try:
1290 func()
1292 func()
1291 finally:
1293 finally:
1292 readline.read_history_file(self.histfile)
1294 readline.read_history_file(self.histfile)
1293 return wrapper
1295 return wrapper
1294
1296
1295
1297
1296 def pre_readline(self):
1298 def pre_readline(self):
1297 """readline hook to be used at the start of each line.
1299 """readline hook to be used at the start of each line.
1298
1300
1299 Currently it handles auto-indent only."""
1301 Currently it handles auto-indent only."""
1300
1302
1301 #debugx('self.indent_current_nsp','pre_readline:')
1303 #debugx('self.indent_current_nsp','pre_readline:')
1302
1304
1303 if self.rl_do_indent:
1305 if self.rl_do_indent:
1304 self.readline.insert_text(self.indent_current_str())
1306 self.readline.insert_text(self.indent_current_str())
1305 if self.rl_next_input is not None:
1307 if self.rl_next_input is not None:
1306 self.readline.insert_text(self.rl_next_input)
1308 self.readline.insert_text(self.rl_next_input)
1307 self.rl_next_input = None
1309 self.rl_next_input = None
1308
1310
1309 def init_readline(self):
1311 def init_readline(self):
1310 """Command history completion/saving/reloading."""
1312 """Command history completion/saving/reloading."""
1311
1313
1312
1314
1313 import IPython.rlineimpl as readline
1315 import IPython.rlineimpl as readline
1314
1316
1315 if not readline.have_readline:
1317 if not readline.have_readline:
1316 self.has_readline = 0
1318 self.has_readline = 0
1317 self.readline = None
1319 self.readline = None
1318 # no point in bugging windows users with this every time:
1320 # no point in bugging windows users with this every time:
1319 warn('Readline services not available on this platform.')
1321 warn('Readline services not available on this platform.')
1320 else:
1322 else:
1321 sys.modules['readline'] = readline
1323 sys.modules['readline'] = readline
1322 import atexit
1324 import atexit
1323 from IPython.completer import IPCompleter
1325 from IPython.completer import IPCompleter
1324 self.Completer = IPCompleter(self,
1326 self.Completer = IPCompleter(self,
1325 self.user_ns,
1327 self.user_ns,
1326 self.user_global_ns,
1328 self.user_global_ns,
1327 self.rc.readline_omit__names,
1329 self.rc.readline_omit__names,
1328 self.alias_table)
1330 self.alias_table)
1329 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1331 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1330 self.strdispatchers['complete_command'] = sdisp
1332 self.strdispatchers['complete_command'] = sdisp
1331 self.Completer.custom_completers = sdisp
1333 self.Completer.custom_completers = sdisp
1332 # Platform-specific configuration
1334 # Platform-specific configuration
1333 if os.name == 'nt':
1335 if os.name == 'nt':
1334 self.readline_startup_hook = readline.set_pre_input_hook
1336 self.readline_startup_hook = readline.set_pre_input_hook
1335 else:
1337 else:
1336 self.readline_startup_hook = readline.set_startup_hook
1338 self.readline_startup_hook = readline.set_startup_hook
1337
1339
1338 # Load user's initrc file (readline config)
1340 # Load user's initrc file (readline config)
1339 # Or if libedit is used, load editrc.
1341 # Or if libedit is used, load editrc.
1340 inputrc_name = os.environ.get('INPUTRC')
1342 inputrc_name = os.environ.get('INPUTRC')
1341 if inputrc_name is None:
1343 if inputrc_name is None:
1342 home_dir = get_home_dir()
1344 home_dir = get_home_dir()
1343 if home_dir is not None:
1345 if home_dir is not None:
1344 inputrc_name = '.inputrc'
1346 inputrc_name = '.inputrc'
1345 if readline.uses_libedit:
1347 if readline.uses_libedit:
1346 inputrc_name = '.editrc'
1348 inputrc_name = '.editrc'
1347 inputrc_name = os.path.join(home_dir, inputrc_name)
1349 inputrc_name = os.path.join(home_dir, inputrc_name)
1348 if os.path.isfile(inputrc_name):
1350 if os.path.isfile(inputrc_name):
1349 try:
1351 try:
1350 readline.read_init_file(inputrc_name)
1352 readline.read_init_file(inputrc_name)
1351 except:
1353 except:
1352 warn('Problems reading readline initialization file <%s>'
1354 warn('Problems reading readline initialization file <%s>'
1353 % inputrc_name)
1355 % inputrc_name)
1354
1356
1355 self.has_readline = 1
1357 self.has_readline = 1
1356 self.readline = readline
1358 self.readline = readline
1357 # save this in sys so embedded copies can restore it properly
1359 # save this in sys so embedded copies can restore it properly
1358 sys.ipcompleter = self.Completer.complete
1360 sys.ipcompleter = self.Completer.complete
1359 self.set_completer()
1361 self.set_completer()
1360
1362
1361 # Configure readline according to user's prefs
1363 # Configure readline according to user's prefs
1362 # This is only done if GNU readline is being used. If libedit
1364 # This is only done if GNU readline is being used. If libedit
1363 # is being used (as on Leopard) the readline config is
1365 # is being used (as on Leopard) the readline config is
1364 # not run as the syntax for libedit is different.
1366 # not run as the syntax for libedit is different.
1365 if not readline.uses_libedit:
1367 if not readline.uses_libedit:
1366 for rlcommand in self.rc.readline_parse_and_bind:
1368 for rlcommand in self.rc.readline_parse_and_bind:
1367 readline.parse_and_bind(rlcommand)
1369 readline.parse_and_bind(rlcommand)
1368
1370
1369 # remove some chars from the delimiters list
1371 # remove some chars from the delimiters list
1370 delims = readline.get_completer_delims()
1372 delims = readline.get_completer_delims()
1371 delims = delims.translate(string._idmap,
1373 delims = delims.translate(string._idmap,
1372 self.rc.readline_remove_delims)
1374 self.rc.readline_remove_delims)
1373 readline.set_completer_delims(delims)
1375 readline.set_completer_delims(delims)
1374 # otherwise we end up with a monster history after a while:
1376 # otherwise we end up with a monster history after a while:
1375 readline.set_history_length(1000)
1377 readline.set_history_length(1000)
1376 try:
1378 try:
1377 #print '*** Reading readline history' # dbg
1379 #print '*** Reading readline history' # dbg
1378 readline.read_history_file(self.histfile)
1380 readline.read_history_file(self.histfile)
1379 except IOError:
1381 except IOError:
1380 pass # It doesn't exist yet.
1382 pass # It doesn't exist yet.
1381
1383
1382 atexit.register(self.atexit_operations)
1384 atexit.register(self.atexit_operations)
1383 del atexit
1385 del atexit
1384
1386
1385 # Configure auto-indent for all platforms
1387 # Configure auto-indent for all platforms
1386 self.set_autoindent(self.rc.autoindent)
1388 self.set_autoindent(self.rc.autoindent)
1387
1389
1388 def ask_yes_no(self,prompt,default=True):
1390 def ask_yes_no(self,prompt,default=True):
1389 if self.rc.quiet:
1391 if self.rc.quiet:
1390 return True
1392 return True
1391 return ask_yes_no(prompt,default)
1393 return ask_yes_no(prompt,default)
1392
1394
1393 def _should_recompile(self,e):
1395 def _should_recompile(self,e):
1394 """Utility routine for edit_syntax_error"""
1396 """Utility routine for edit_syntax_error"""
1395
1397
1396 if e.filename in ('<ipython console>','<input>','<string>',
1398 if e.filename in ('<ipython console>','<input>','<string>',
1397 '<console>','<BackgroundJob compilation>',
1399 '<console>','<BackgroundJob compilation>',
1398 None):
1400 None):
1399
1401
1400 return False
1402 return False
1401 try:
1403 try:
1402 if (self.rc.autoedit_syntax and
1404 if (self.rc.autoedit_syntax and
1403 not self.ask_yes_no('Return to editor to correct syntax error? '
1405 not self.ask_yes_no('Return to editor to correct syntax error? '
1404 '[Y/n] ','y')):
1406 '[Y/n] ','y')):
1405 return False
1407 return False
1406 except EOFError:
1408 except EOFError:
1407 return False
1409 return False
1408
1410
1409 def int0(x):
1411 def int0(x):
1410 try:
1412 try:
1411 return int(x)
1413 return int(x)
1412 except TypeError:
1414 except TypeError:
1413 return 0
1415 return 0
1414 # always pass integer line and offset values to editor hook
1416 # always pass integer line and offset values to editor hook
1415 self.hooks.fix_error_editor(e.filename,
1417 self.hooks.fix_error_editor(e.filename,
1416 int0(e.lineno),int0(e.offset),e.msg)
1418 int0(e.lineno),int0(e.offset),e.msg)
1417 return True
1419 return True
1418
1420
1419 def edit_syntax_error(self):
1421 def edit_syntax_error(self):
1420 """The bottom half of the syntax error handler called in the main loop.
1422 """The bottom half of the syntax error handler called in the main loop.
1421
1423
1422 Loop until syntax error is fixed or user cancels.
1424 Loop until syntax error is fixed or user cancels.
1423 """
1425 """
1424
1426
1425 while self.SyntaxTB.last_syntax_error:
1427 while self.SyntaxTB.last_syntax_error:
1426 # copy and clear last_syntax_error
1428 # copy and clear last_syntax_error
1427 err = self.SyntaxTB.clear_err_state()
1429 err = self.SyntaxTB.clear_err_state()
1428 if not self._should_recompile(err):
1430 if not self._should_recompile(err):
1429 return
1431 return
1430 try:
1432 try:
1431 # may set last_syntax_error again if a SyntaxError is raised
1433 # may set last_syntax_error again if a SyntaxError is raised
1432 self.safe_execfile(err.filename,self.user_ns)
1434 self.safe_execfile(err.filename,self.user_ns)
1433 except:
1435 except:
1434 self.showtraceback()
1436 self.showtraceback()
1435 else:
1437 else:
1436 try:
1438 try:
1437 f = file(err.filename)
1439 f = file(err.filename)
1438 try:
1440 try:
1439 sys.displayhook(f.read())
1441 sys.displayhook(f.read())
1440 finally:
1442 finally:
1441 f.close()
1443 f.close()
1442 except:
1444 except:
1443 self.showtraceback()
1445 self.showtraceback()
1444
1446
1445 def showsyntaxerror(self, filename=None):
1447 def showsyntaxerror(self, filename=None):
1446 """Display the syntax error that just occurred.
1448 """Display the syntax error that just occurred.
1447
1449
1448 This doesn't display a stack trace because there isn't one.
1450 This doesn't display a stack trace because there isn't one.
1449
1451
1450 If a filename is given, it is stuffed in the exception instead
1452 If a filename is given, it is stuffed in the exception instead
1451 of what was there before (because Python's parser always uses
1453 of what was there before (because Python's parser always uses
1452 "<string>" when reading from a string).
1454 "<string>" when reading from a string).
1453 """
1455 """
1454 etype, value, last_traceback = sys.exc_info()
1456 etype, value, last_traceback = sys.exc_info()
1455
1457
1456 # See note about these variables in showtraceback() below
1458 # See note about these variables in showtraceback() below
1457 sys.last_type = etype
1459 sys.last_type = etype
1458 sys.last_value = value
1460 sys.last_value = value
1459 sys.last_traceback = last_traceback
1461 sys.last_traceback = last_traceback
1460
1462
1461 if filename and etype is SyntaxError:
1463 if filename and etype is SyntaxError:
1462 # Work hard to stuff the correct filename in the exception
1464 # Work hard to stuff the correct filename in the exception
1463 try:
1465 try:
1464 msg, (dummy_filename, lineno, offset, line) = value
1466 msg, (dummy_filename, lineno, offset, line) = value
1465 except:
1467 except:
1466 # Not the format we expect; leave it alone
1468 # Not the format we expect; leave it alone
1467 pass
1469 pass
1468 else:
1470 else:
1469 # Stuff in the right filename
1471 # Stuff in the right filename
1470 try:
1472 try:
1471 # Assume SyntaxError is a class exception
1473 # Assume SyntaxError is a class exception
1472 value = SyntaxError(msg, (filename, lineno, offset, line))
1474 value = SyntaxError(msg, (filename, lineno, offset, line))
1473 except:
1475 except:
1474 # If that failed, assume SyntaxError is a string
1476 # If that failed, assume SyntaxError is a string
1475 value = msg, (filename, lineno, offset, line)
1477 value = msg, (filename, lineno, offset, line)
1476 self.SyntaxTB(etype,value,[])
1478 self.SyntaxTB(etype,value,[])
1477
1479
1478 def debugger(self,force=False):
1480 def debugger(self,force=False):
1479 """Call the pydb/pdb debugger.
1481 """Call the pydb/pdb debugger.
1480
1482
1481 Keywords:
1483 Keywords:
1482
1484
1483 - force(False): by default, this routine checks the instance call_pdb
1485 - force(False): by default, this routine checks the instance call_pdb
1484 flag and does not actually invoke the debugger if the flag is false.
1486 flag and does not actually invoke the debugger if the flag is false.
1485 The 'force' option forces the debugger to activate even if the flag
1487 The 'force' option forces the debugger to activate even if the flag
1486 is false.
1488 is false.
1487 """
1489 """
1488
1490
1489 if not (force or self.call_pdb):
1491 if not (force or self.call_pdb):
1490 return
1492 return
1491
1493
1492 if not hasattr(sys,'last_traceback'):
1494 if not hasattr(sys,'last_traceback'):
1493 error('No traceback has been produced, nothing to debug.')
1495 error('No traceback has been produced, nothing to debug.')
1494 return
1496 return
1495
1497
1496 # use pydb if available
1498 # use pydb if available
1497 if Debugger.has_pydb:
1499 if Debugger.has_pydb:
1498 from pydb import pm
1500 from pydb import pm
1499 else:
1501 else:
1500 # fallback to our internal debugger
1502 # fallback to our internal debugger
1501 pm = lambda : self.InteractiveTB.debugger(force=True)
1503 pm = lambda : self.InteractiveTB.debugger(force=True)
1502 self.history_saving_wrapper(pm)()
1504 self.history_saving_wrapper(pm)()
1503
1505
1504 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1506 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1505 """Display the exception that just occurred.
1507 """Display the exception that just occurred.
1506
1508
1507 If nothing is known about the exception, this is the method which
1509 If nothing is known about the exception, this is the method which
1508 should be used throughout the code for presenting user tracebacks,
1510 should be used throughout the code for presenting user tracebacks,
1509 rather than directly invoking the InteractiveTB object.
1511 rather than directly invoking the InteractiveTB object.
1510
1512
1511 A specific showsyntaxerror() also exists, but this method can take
1513 A specific showsyntaxerror() also exists, but this method can take
1512 care of calling it if needed, so unless you are explicitly catching a
1514 care of calling it if needed, so unless you are explicitly catching a
1513 SyntaxError exception, don't try to analyze the stack manually and
1515 SyntaxError exception, don't try to analyze the stack manually and
1514 simply call this method."""
1516 simply call this method."""
1515
1517
1516
1518
1517 # Though this won't be called by syntax errors in the input line,
1519 # Though this won't be called by syntax errors in the input line,
1518 # there may be SyntaxError cases whith imported code.
1520 # there may be SyntaxError cases whith imported code.
1519
1521
1520 try:
1522 try:
1521 if exc_tuple is None:
1523 if exc_tuple is None:
1522 etype, value, tb = sys.exc_info()
1524 etype, value, tb = sys.exc_info()
1523 else:
1525 else:
1524 etype, value, tb = exc_tuple
1526 etype, value, tb = exc_tuple
1525
1527
1526 if etype is SyntaxError:
1528 if etype is SyntaxError:
1527 self.showsyntaxerror(filename)
1529 self.showsyntaxerror(filename)
1528 elif etype is IPython.ipapi.UsageError:
1530 elif etype is IPython.ipapi.UsageError:
1529 print "UsageError:", value
1531 print "UsageError:", value
1530 else:
1532 else:
1531 # WARNING: these variables are somewhat deprecated and not
1533 # WARNING: these variables are somewhat deprecated and not
1532 # necessarily safe to use in a threaded environment, but tools
1534 # necessarily safe to use in a threaded environment, but tools
1533 # like pdb depend on their existence, so let's set them. If we
1535 # like pdb depend on their existence, so let's set them. If we
1534 # find problems in the field, we'll need to revisit their use.
1536 # find problems in the field, we'll need to revisit their use.
1535 sys.last_type = etype
1537 sys.last_type = etype
1536 sys.last_value = value
1538 sys.last_value = value
1537 sys.last_traceback = tb
1539 sys.last_traceback = tb
1538
1540
1539 if etype in self.custom_exceptions:
1541 if etype in self.custom_exceptions:
1540 self.CustomTB(etype,value,tb)
1542 self.CustomTB(etype,value,tb)
1541 else:
1543 else:
1542 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1544 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1543 if self.InteractiveTB.call_pdb and self.has_readline:
1545 if self.InteractiveTB.call_pdb and self.has_readline:
1544 # pdb mucks up readline, fix it back
1546 # pdb mucks up readline, fix it back
1545 self.set_completer()
1547 self.set_completer()
1546 except KeyboardInterrupt:
1548 except KeyboardInterrupt:
1547 self.write("\nKeyboardInterrupt\n")
1549 self.write("\nKeyboardInterrupt\n")
1548
1550
1549
1551
1550
1552
1551 def mainloop(self,banner=None):
1553 def mainloop(self,banner=None):
1552 """Creates the local namespace and starts the mainloop.
1554 """Creates the local namespace and starts the mainloop.
1553
1555
1554 If an optional banner argument is given, it will override the
1556 If an optional banner argument is given, it will override the
1555 internally created default banner."""
1557 internally created default banner."""
1556
1558
1557 if self.rc.c: # Emulate Python's -c option
1559 if self.rc.c: # Emulate Python's -c option
1558 self.exec_init_cmd()
1560 self.exec_init_cmd()
1559 if banner is None:
1561 if banner is None:
1560 if not self.rc.banner:
1562 if not self.rc.banner:
1561 banner = ''
1563 banner = ''
1562 # banner is string? Use it directly!
1564 # banner is string? Use it directly!
1563 elif isinstance(self.rc.banner,basestring):
1565 elif isinstance(self.rc.banner,basestring):
1564 banner = self.rc.banner
1566 banner = self.rc.banner
1565 else:
1567 else:
1566 banner = self.BANNER+self.banner2
1568 banner = self.BANNER+self.banner2
1567
1569
1568 while 1:
1570 while 1:
1569 try:
1571 try:
1570 self.interact(banner)
1572 self.interact(banner)
1571 #self.interact_with_readline()
1573 #self.interact_with_readline()
1572 # XXX for testing of a readline-decoupled repl loop, call interact_with_readline above
1574 # XXX for testing of a readline-decoupled repl loop, call interact_with_readline above
1573
1575
1574 break
1576 break
1575 except KeyboardInterrupt:
1577 except KeyboardInterrupt:
1576 # this should not be necessary, but KeyboardInterrupt
1578 # this should not be necessary, but KeyboardInterrupt
1577 # handling seems rather unpredictable...
1579 # handling seems rather unpredictable...
1578 self.write("\nKeyboardInterrupt in interact()\n")
1580 self.write("\nKeyboardInterrupt in interact()\n")
1579
1581
1580 def exec_init_cmd(self):
1582 def exec_init_cmd(self):
1581 """Execute a command given at the command line.
1583 """Execute a command given at the command line.
1582
1584
1583 This emulates Python's -c option."""
1585 This emulates Python's -c option."""
1584
1586
1585 #sys.argv = ['-c']
1587 #sys.argv = ['-c']
1586 self.push(self.prefilter(self.rc.c, False))
1588 self.push(self.prefilter(self.rc.c, False))
1587 if not self.rc.interact:
1589 if not self.rc.interact:
1588 self.exit_now = True
1590 self.exit_now = True
1589
1591
1590 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1592 def embed_mainloop(self,header='',local_ns=None,global_ns=None,stack_depth=0):
1591 """Embeds IPython into a running python program.
1593 """Embeds IPython into a running python program.
1592
1594
1593 Input:
1595 Input:
1594
1596
1595 - header: An optional header message can be specified.
1597 - header: An optional header message can be specified.
1596
1598
1597 - local_ns, global_ns: working namespaces. If given as None, the
1599 - local_ns, global_ns: working namespaces. If given as None, the
1598 IPython-initialized one is updated with __main__.__dict__, so that
1600 IPython-initialized one is updated with __main__.__dict__, so that
1599 program variables become visible but user-specific configuration
1601 program variables become visible but user-specific configuration
1600 remains possible.
1602 remains possible.
1601
1603
1602 - stack_depth: specifies how many levels in the stack to go to
1604 - stack_depth: specifies how many levels in the stack to go to
1603 looking for namespaces (when local_ns and global_ns are None). This
1605 looking for namespaces (when local_ns and global_ns are None). This
1604 allows an intermediate caller to make sure that this function gets
1606 allows an intermediate caller to make sure that this function gets
1605 the namespace from the intended level in the stack. By default (0)
1607 the namespace from the intended level in the stack. By default (0)
1606 it will get its locals and globals from the immediate caller.
1608 it will get its locals and globals from the immediate caller.
1607
1609
1608 Warning: it's possible to use this in a program which is being run by
1610 Warning: it's possible to use this in a program which is being run by
1609 IPython itself (via %run), but some funny things will happen (a few
1611 IPython itself (via %run), but some funny things will happen (a few
1610 globals get overwritten). In the future this will be cleaned up, as
1612 globals get overwritten). In the future this will be cleaned up, as
1611 there is no fundamental reason why it can't work perfectly."""
1613 there is no fundamental reason why it can't work perfectly."""
1612
1614
1613 # Get locals and globals from caller
1615 # Get locals and globals from caller
1614 if local_ns is None or global_ns is None:
1616 if local_ns is None or global_ns is None:
1615 call_frame = sys._getframe(stack_depth).f_back
1617 call_frame = sys._getframe(stack_depth).f_back
1616
1618
1617 if local_ns is None:
1619 if local_ns is None:
1618 local_ns = call_frame.f_locals
1620 local_ns = call_frame.f_locals
1619 if global_ns is None:
1621 if global_ns is None:
1620 global_ns = call_frame.f_globals
1622 global_ns = call_frame.f_globals
1621
1623
1622 # Update namespaces and fire up interpreter
1624 # Update namespaces and fire up interpreter
1623
1625
1624 # The global one is easy, we can just throw it in
1626 # The global one is easy, we can just throw it in
1625 self.user_global_ns = global_ns
1627 self.user_global_ns = global_ns
1626
1628
1627 # but the user/local one is tricky: ipython needs it to store internal
1629 # but the user/local one is tricky: ipython needs it to store internal
1628 # data, but we also need the locals. We'll copy locals in the user
1630 # data, but we also need the locals. We'll copy locals in the user
1629 # one, but will track what got copied so we can delete them at exit.
1631 # one, but will track what got copied so we can delete them at exit.
1630 # This is so that a later embedded call doesn't see locals from a
1632 # This is so that a later embedded call doesn't see locals from a
1631 # previous call (which most likely existed in a separate scope).
1633 # previous call (which most likely existed in a separate scope).
1632 local_varnames = local_ns.keys()
1634 local_varnames = local_ns.keys()
1633 self.user_ns.update(local_ns)
1635 self.user_ns.update(local_ns)
1634
1636
1635 # Patch for global embedding to make sure that things don't overwrite
1637 # Patch for global embedding to make sure that things don't overwrite
1636 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1638 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
1637 # FIXME. Test this a bit more carefully (the if.. is new)
1639 # FIXME. Test this a bit more carefully (the if.. is new)
1638 if local_ns is None and global_ns is None:
1640 if local_ns is None and global_ns is None:
1639 self.user_global_ns.update(__main__.__dict__)
1641 self.user_global_ns.update(__main__.__dict__)
1640
1642
1641 # make sure the tab-completer has the correct frame information, so it
1643 # make sure the tab-completer has the correct frame information, so it
1642 # actually completes using the frame's locals/globals
1644 # actually completes using the frame's locals/globals
1643 self.set_completer_frame()
1645 self.set_completer_frame()
1644
1646
1645 # before activating the interactive mode, we need to make sure that
1647 # before activating the interactive mode, we need to make sure that
1646 # all names in the builtin namespace needed by ipython point to
1648 # all names in the builtin namespace needed by ipython point to
1647 # ourselves, and not to other instances.
1649 # ourselves, and not to other instances.
1648 self.add_builtins()
1650 self.add_builtins()
1649
1651
1650 self.interact(header)
1652 self.interact(header)
1651
1653
1652 # now, purge out the user namespace from anything we might have added
1654 # now, purge out the user namespace from anything we might have added
1653 # from the caller's local namespace
1655 # from the caller's local namespace
1654 delvar = self.user_ns.pop
1656 delvar = self.user_ns.pop
1655 for var in local_varnames:
1657 for var in local_varnames:
1656 delvar(var,None)
1658 delvar(var,None)
1657 # and clean builtins we may have overridden
1659 # and clean builtins we may have overridden
1658 self.clean_builtins()
1660 self.clean_builtins()
1659
1661
1660 def interact_prompt(self):
1662 def interact_prompt(self):
1661 """ Print the prompt (in read-eval-print loop)
1663 """ Print the prompt (in read-eval-print loop)
1662
1664
1663 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1665 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1664 used in standard IPython flow.
1666 used in standard IPython flow.
1665 """
1667 """
1666 if self.more:
1668 if self.more:
1667 try:
1669 try:
1668 prompt = self.hooks.generate_prompt(True)
1670 prompt = self.hooks.generate_prompt(True)
1669 except:
1671 except:
1670 self.showtraceback()
1672 self.showtraceback()
1671 if self.autoindent:
1673 if self.autoindent:
1672 self.rl_do_indent = True
1674 self.rl_do_indent = True
1673
1675
1674 else:
1676 else:
1675 try:
1677 try:
1676 prompt = self.hooks.generate_prompt(False)
1678 prompt = self.hooks.generate_prompt(False)
1677 except:
1679 except:
1678 self.showtraceback()
1680 self.showtraceback()
1679 self.write(prompt)
1681 self.write(prompt)
1680
1682
1681 def interact_handle_input(self,line):
1683 def interact_handle_input(self,line):
1682 """ Handle the input line (in read-eval-print loop)
1684 """ Handle the input line (in read-eval-print loop)
1683
1685
1684 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1686 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1685 used in standard IPython flow.
1687 used in standard IPython flow.
1686 """
1688 """
1687 if line.lstrip() == line:
1689 if line.lstrip() == line:
1688 self.shadowhist.add(line.strip())
1690 self.shadowhist.add(line.strip())
1689 lineout = self.prefilter(line,self.more)
1691 lineout = self.prefilter(line,self.more)
1690
1692
1691 if line.strip():
1693 if line.strip():
1692 if self.more:
1694 if self.more:
1693 self.input_hist_raw[-1] += '%s\n' % line
1695 self.input_hist_raw[-1] += '%s\n' % line
1694 else:
1696 else:
1695 self.input_hist_raw.append('%s\n' % line)
1697 self.input_hist_raw.append('%s\n' % line)
1696
1698
1697
1699
1698 self.more = self.push(lineout)
1700 self.more = self.push(lineout)
1699 if (self.SyntaxTB.last_syntax_error and
1701 if (self.SyntaxTB.last_syntax_error and
1700 self.rc.autoedit_syntax):
1702 self.rc.autoedit_syntax):
1701 self.edit_syntax_error()
1703 self.edit_syntax_error()
1702
1704
1703 def interact_with_readline(self):
1705 def interact_with_readline(self):
1704 """ Demo of using interact_handle_input, interact_prompt
1706 """ Demo of using interact_handle_input, interact_prompt
1705
1707
1706 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1708 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1707 it should work like this.
1709 it should work like this.
1708 """
1710 """
1709 self.readline_startup_hook(self.pre_readline)
1711 self.readline_startup_hook(self.pre_readline)
1710 while not self.exit_now:
1712 while not self.exit_now:
1711 self.interact_prompt()
1713 self.interact_prompt()
1712 if self.more:
1714 if self.more:
1713 self.rl_do_indent = True
1715 self.rl_do_indent = True
1714 else:
1716 else:
1715 self.rl_do_indent = False
1717 self.rl_do_indent = False
1716 line = raw_input_original().decode(self.stdin_encoding)
1718 line = raw_input_original().decode(self.stdin_encoding)
1717 self.interact_handle_input(line)
1719 self.interact_handle_input(line)
1718
1720
1719
1721
1720 def interact(self, banner=None):
1722 def interact(self, banner=None):
1721 """Closely emulate the interactive Python console.
1723 """Closely emulate the interactive Python console.
1722
1724
1723 The optional banner argument specify the banner to print
1725 The optional banner argument specify the banner to print
1724 before the first interaction; by default it prints a banner
1726 before the first interaction; by default it prints a banner
1725 similar to the one printed by the real Python interpreter,
1727 similar to the one printed by the real Python interpreter,
1726 followed by the current class name in parentheses (so as not
1728 followed by the current class name in parentheses (so as not
1727 to confuse this with the real interpreter -- since it's so
1729 to confuse this with the real interpreter -- since it's so
1728 close!).
1730 close!).
1729
1731
1730 """
1732 """
1731
1733
1732 if self.exit_now:
1734 if self.exit_now:
1733 # batch run -> do not interact
1735 # batch run -> do not interact
1734 return
1736 return
1735 cprt = 'Type "copyright", "credits" or "license" for more information.'
1737 cprt = 'Type "copyright", "credits" or "license" for more information.'
1736 if banner is None:
1738 if banner is None:
1737 self.write("Python %s on %s\n%s\n(%s)\n" %
1739 self.write("Python %s on %s\n%s\n(%s)\n" %
1738 (sys.version, sys.platform, cprt,
1740 (sys.version, sys.platform, cprt,
1739 self.__class__.__name__))
1741 self.__class__.__name__))
1740 else:
1742 else:
1741 self.write(banner)
1743 self.write(banner)
1742
1744
1743 more = 0
1745 more = 0
1744
1746
1745 # Mark activity in the builtins
1747 # Mark activity in the builtins
1746 __builtin__.__dict__['__IPYTHON__active'] += 1
1748 __builtin__.__dict__['__IPYTHON__active'] += 1
1747
1749
1748 if self.has_readline:
1750 if self.has_readline:
1749 self.readline_startup_hook(self.pre_readline)
1751 self.readline_startup_hook(self.pre_readline)
1750 # exit_now is set by a call to %Exit or %Quit
1752 # exit_now is set by a call to %Exit or %Quit
1751
1753
1752 while not self.exit_now:
1754 while not self.exit_now:
1753 self.hooks.pre_prompt_hook()
1755 self.hooks.pre_prompt_hook()
1754 if more:
1756 if more:
1755 try:
1757 try:
1756 prompt = self.hooks.generate_prompt(True)
1758 prompt = self.hooks.generate_prompt(True)
1757 except:
1759 except:
1758 self.showtraceback()
1760 self.showtraceback()
1759 if self.autoindent:
1761 if self.autoindent:
1760 self.rl_do_indent = True
1762 self.rl_do_indent = True
1761
1763
1762 else:
1764 else:
1763 try:
1765 try:
1764 prompt = self.hooks.generate_prompt(False)
1766 prompt = self.hooks.generate_prompt(False)
1765 except:
1767 except:
1766 self.showtraceback()
1768 self.showtraceback()
1767 try:
1769 try:
1768 line = self.raw_input(prompt,more)
1770 line = self.raw_input(prompt,more)
1769 if self.exit_now:
1771 if self.exit_now:
1770 # quick exit on sys.std[in|out] close
1772 # quick exit on sys.std[in|out] close
1771 break
1773 break
1772 if self.autoindent:
1774 if self.autoindent:
1773 self.rl_do_indent = False
1775 self.rl_do_indent = False
1774
1776
1775 except KeyboardInterrupt:
1777 except KeyboardInterrupt:
1776 #double-guard against keyboardinterrupts during kbdint handling
1778 #double-guard against keyboardinterrupts during kbdint handling
1777 try:
1779 try:
1778 self.write('\nKeyboardInterrupt\n')
1780 self.write('\nKeyboardInterrupt\n')
1779 self.resetbuffer()
1781 self.resetbuffer()
1780 # keep cache in sync with the prompt counter:
1782 # keep cache in sync with the prompt counter:
1781 self.outputcache.prompt_count -= 1
1783 self.outputcache.prompt_count -= 1
1782
1784
1783 if self.autoindent:
1785 if self.autoindent:
1784 self.indent_current_nsp = 0
1786 self.indent_current_nsp = 0
1785 more = 0
1787 more = 0
1786 except KeyboardInterrupt:
1788 except KeyboardInterrupt:
1787 pass
1789 pass
1788 except EOFError:
1790 except EOFError:
1789 if self.autoindent:
1791 if self.autoindent:
1790 self.rl_do_indent = False
1792 self.rl_do_indent = False
1791 self.readline_startup_hook(None)
1793 self.readline_startup_hook(None)
1792 self.write('\n')
1794 self.write('\n')
1793 self.exit()
1795 self.exit()
1794 except bdb.BdbQuit:
1796 except bdb.BdbQuit:
1795 warn('The Python debugger has exited with a BdbQuit exception.\n'
1797 warn('The Python debugger has exited with a BdbQuit exception.\n'
1796 'Because of how pdb handles the stack, it is impossible\n'
1798 'Because of how pdb handles the stack, it is impossible\n'
1797 'for IPython to properly format this particular exception.\n'
1799 'for IPython to properly format this particular exception.\n'
1798 'IPython will resume normal operation.')
1800 'IPython will resume normal operation.')
1799 except:
1801 except:
1800 # exceptions here are VERY RARE, but they can be triggered
1802 # exceptions here are VERY RARE, but they can be triggered
1801 # asynchronously by signal handlers, for example.
1803 # asynchronously by signal handlers, for example.
1802 self.showtraceback()
1804 self.showtraceback()
1803 else:
1805 else:
1804 more = self.push(line)
1806 more = self.push(line)
1805 if (self.SyntaxTB.last_syntax_error and
1807 if (self.SyntaxTB.last_syntax_error and
1806 self.rc.autoedit_syntax):
1808 self.rc.autoedit_syntax):
1807 self.edit_syntax_error()
1809 self.edit_syntax_error()
1808
1810
1809 # We are off again...
1811 # We are off again...
1810 __builtin__.__dict__['__IPYTHON__active'] -= 1
1812 __builtin__.__dict__['__IPYTHON__active'] -= 1
1811
1813
1812 def excepthook(self, etype, value, tb):
1814 def excepthook(self, etype, value, tb):
1813 """One more defense for GUI apps that call sys.excepthook.
1815 """One more defense for GUI apps that call sys.excepthook.
1814
1816
1815 GUI frameworks like wxPython trap exceptions and call
1817 GUI frameworks like wxPython trap exceptions and call
1816 sys.excepthook themselves. I guess this is a feature that
1818 sys.excepthook themselves. I guess this is a feature that
1817 enables them to keep running after exceptions that would
1819 enables them to keep running after exceptions that would
1818 otherwise kill their mainloop. This is a bother for IPython
1820 otherwise kill their mainloop. This is a bother for IPython
1819 which excepts to catch all of the program exceptions with a try:
1821 which excepts to catch all of the program exceptions with a try:
1820 except: statement.
1822 except: statement.
1821
1823
1822 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1824 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1823 any app directly invokes sys.excepthook, it will look to the user like
1825 any app directly invokes sys.excepthook, it will look to the user like
1824 IPython crashed. In order to work around this, we can disable the
1826 IPython crashed. In order to work around this, we can disable the
1825 CrashHandler and replace it with this excepthook instead, which prints a
1827 CrashHandler and replace it with this excepthook instead, which prints a
1826 regular traceback using our InteractiveTB. In this fashion, apps which
1828 regular traceback using our InteractiveTB. In this fashion, apps which
1827 call sys.excepthook will generate a regular-looking exception from
1829 call sys.excepthook will generate a regular-looking exception from
1828 IPython, and the CrashHandler will only be triggered by real IPython
1830 IPython, and the CrashHandler will only be triggered by real IPython
1829 crashes.
1831 crashes.
1830
1832
1831 This hook should be used sparingly, only in places which are not likely
1833 This hook should be used sparingly, only in places which are not likely
1832 to be true IPython errors.
1834 to be true IPython errors.
1833 """
1835 """
1834 self.showtraceback((etype,value,tb),tb_offset=0)
1836 self.showtraceback((etype,value,tb),tb_offset=0)
1835
1837
1836 def expand_aliases(self,fn,rest):
1838 def expand_aliases(self,fn,rest):
1837 """ Expand multiple levels of aliases:
1839 """ Expand multiple levels of aliases:
1838
1840
1839 if:
1841 if:
1840
1842
1841 alias foo bar /tmp
1843 alias foo bar /tmp
1842 alias baz foo
1844 alias baz foo
1843
1845
1844 then:
1846 then:
1845
1847
1846 baz huhhahhei -> bar /tmp huhhahhei
1848 baz huhhahhei -> bar /tmp huhhahhei
1847
1849
1848 """
1850 """
1849 line = fn + " " + rest
1851 line = fn + " " + rest
1850
1852
1851 done = Set()
1853 done = Set()
1852 while 1:
1854 while 1:
1853 pre,fn,rest = prefilter.splitUserInput(line,
1855 pre,fn,rest = prefilter.splitUserInput(line,
1854 prefilter.shell_line_split)
1856 prefilter.shell_line_split)
1855 if fn in self.alias_table:
1857 if fn in self.alias_table:
1856 if fn in done:
1858 if fn in done:
1857 warn("Cyclic alias definition, repeated '%s'" % fn)
1859 warn("Cyclic alias definition, repeated '%s'" % fn)
1858 return ""
1860 return ""
1859 done.add(fn)
1861 done.add(fn)
1860
1862
1861 l2 = self.transform_alias(fn,rest)
1863 l2 = self.transform_alias(fn,rest)
1862 # dir -> dir
1864 # dir -> dir
1863 # print "alias",line, "->",l2 #dbg
1865 # print "alias",line, "->",l2 #dbg
1864 if l2 == line:
1866 if l2 == line:
1865 break
1867 break
1866 # ls -> ls -F should not recurse forever
1868 # ls -> ls -F should not recurse forever
1867 if l2.split(None,1)[0] == line.split(None,1)[0]:
1869 if l2.split(None,1)[0] == line.split(None,1)[0]:
1868 line = l2
1870 line = l2
1869 break
1871 break
1870
1872
1871 line=l2
1873 line=l2
1872
1874
1873
1875
1874 # print "al expand to",line #dbg
1876 # print "al expand to",line #dbg
1875 else:
1877 else:
1876 break
1878 break
1877
1879
1878 return line
1880 return line
1879
1881
1880 def transform_alias(self, alias,rest=''):
1882 def transform_alias(self, alias,rest=''):
1881 """ Transform alias to system command string.
1883 """ Transform alias to system command string.
1882 """
1884 """
1883 trg = self.alias_table[alias]
1885 trg = self.alias_table[alias]
1884
1886
1885 nargs,cmd = trg
1887 nargs,cmd = trg
1886 # print trg #dbg
1888 # print trg #dbg
1887 if ' ' in cmd and os.path.isfile(cmd):
1889 if ' ' in cmd and os.path.isfile(cmd):
1888 cmd = '"%s"' % cmd
1890 cmd = '"%s"' % cmd
1889
1891
1890 # Expand the %l special to be the user's input line
1892 # Expand the %l special to be the user's input line
1891 if cmd.find('%l') >= 0:
1893 if cmd.find('%l') >= 0:
1892 cmd = cmd.replace('%l',rest)
1894 cmd = cmd.replace('%l',rest)
1893 rest = ''
1895 rest = ''
1894 if nargs==0:
1896 if nargs==0:
1895 # Simple, argument-less aliases
1897 # Simple, argument-less aliases
1896 cmd = '%s %s' % (cmd,rest)
1898 cmd = '%s %s' % (cmd,rest)
1897 else:
1899 else:
1898 # Handle aliases with positional arguments
1900 # Handle aliases with positional arguments
1899 args = rest.split(None,nargs)
1901 args = rest.split(None,nargs)
1900 if len(args)< nargs:
1902 if len(args)< nargs:
1901 error('Alias <%s> requires %s arguments, %s given.' %
1903 error('Alias <%s> requires %s arguments, %s given.' %
1902 (alias,nargs,len(args)))
1904 (alias,nargs,len(args)))
1903 return None
1905 return None
1904 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1906 cmd = '%s %s' % (cmd % tuple(args[:nargs]),' '.join(args[nargs:]))
1905 # Now call the macro, evaluating in the user's namespace
1907 # Now call the macro, evaluating in the user's namespace
1906 #print 'new command: <%r>' % cmd # dbg
1908 #print 'new command: <%r>' % cmd # dbg
1907 return cmd
1909 return cmd
1908
1910
1909 def call_alias(self,alias,rest=''):
1911 def call_alias(self,alias,rest=''):
1910 """Call an alias given its name and the rest of the line.
1912 """Call an alias given its name and the rest of the line.
1911
1913
1912 This is only used to provide backwards compatibility for users of
1914 This is only used to provide backwards compatibility for users of
1913 ipalias(), use of which is not recommended for anymore."""
1915 ipalias(), use of which is not recommended for anymore."""
1914
1916
1915 # Now call the macro, evaluating in the user's namespace
1917 # Now call the macro, evaluating in the user's namespace
1916 cmd = self.transform_alias(alias, rest)
1918 cmd = self.transform_alias(alias, rest)
1917 try:
1919 try:
1918 self.system(cmd)
1920 self.system(cmd)
1919 except:
1921 except:
1920 self.showtraceback()
1922 self.showtraceback()
1921
1923
1922 def indent_current_str(self):
1924 def indent_current_str(self):
1923 """return the current level of indentation as a string"""
1925 """return the current level of indentation as a string"""
1924 return self.indent_current_nsp * ' '
1926 return self.indent_current_nsp * ' '
1925
1927
1926 def autoindent_update(self,line):
1928 def autoindent_update(self,line):
1927 """Keep track of the indent level."""
1929 """Keep track of the indent level."""
1928
1930
1929 #debugx('line')
1931 #debugx('line')
1930 #debugx('self.indent_current_nsp')
1932 #debugx('self.indent_current_nsp')
1931 if self.autoindent:
1933 if self.autoindent:
1932 if line:
1934 if line:
1933 inisp = num_ini_spaces(line)
1935 inisp = num_ini_spaces(line)
1934 if inisp < self.indent_current_nsp:
1936 if inisp < self.indent_current_nsp:
1935 self.indent_current_nsp = inisp
1937 self.indent_current_nsp = inisp
1936
1938
1937 if line[-1] == ':':
1939 if line[-1] == ':':
1938 self.indent_current_nsp += 4
1940 self.indent_current_nsp += 4
1939 elif dedent_re.match(line):
1941 elif dedent_re.match(line):
1940 self.indent_current_nsp -= 4
1942 self.indent_current_nsp -= 4
1941 else:
1943 else:
1942 self.indent_current_nsp = 0
1944 self.indent_current_nsp = 0
1943
1945
1944 def runlines(self,lines):
1946 def runlines(self,lines):
1945 """Run a string of one or more lines of source.
1947 """Run a string of one or more lines of source.
1946
1948
1947 This method is capable of running a string containing multiple source
1949 This method is capable of running a string containing multiple source
1948 lines, as if they had been entered at the IPython prompt. Since it
1950 lines, as if they had been entered at the IPython prompt. Since it
1949 exposes IPython's processing machinery, the given strings can contain
1951 exposes IPython's processing machinery, the given strings can contain
1950 magic calls (%magic), special shell access (!cmd), etc."""
1952 magic calls (%magic), special shell access (!cmd), etc."""
1951
1953
1952 # We must start with a clean buffer, in case this is run from an
1954 # We must start with a clean buffer, in case this is run from an
1953 # interactive IPython session (via a magic, for example).
1955 # interactive IPython session (via a magic, for example).
1954 self.resetbuffer()
1956 self.resetbuffer()
1955 lines = lines.split('\n')
1957 lines = lines.split('\n')
1956 more = 0
1958 more = 0
1957
1959
1958 for line in lines:
1960 for line in lines:
1959 # skip blank lines so we don't mess up the prompt counter, but do
1961 # skip blank lines so we don't mess up the prompt counter, but do
1960 # NOT skip even a blank line if we are in a code block (more is
1962 # NOT skip even a blank line if we are in a code block (more is
1961 # true)
1963 # true)
1962
1964
1963
1965
1964 if line or more:
1966 if line or more:
1965 # push to raw history, so hist line numbers stay in sync
1967 # push to raw history, so hist line numbers stay in sync
1966 self.input_hist_raw.append("# " + line + "\n")
1968 self.input_hist_raw.append("# " + line + "\n")
1967 more = self.push(self.prefilter(line,more))
1969 more = self.push(self.prefilter(line,more))
1968 # IPython's runsource returns None if there was an error
1970 # IPython's runsource returns None if there was an error
1969 # compiling the code. This allows us to stop processing right
1971 # compiling the code. This allows us to stop processing right
1970 # away, so the user gets the error message at the right place.
1972 # away, so the user gets the error message at the right place.
1971 if more is None:
1973 if more is None:
1972 break
1974 break
1973 else:
1975 else:
1974 self.input_hist_raw.append("\n")
1976 self.input_hist_raw.append("\n")
1975 # final newline in case the input didn't have it, so that the code
1977 # final newline in case the input didn't have it, so that the code
1976 # actually does get executed
1978 # actually does get executed
1977 if more:
1979 if more:
1978 self.push('\n')
1980 self.push('\n')
1979
1981
1980 def runsource(self, source, filename='<input>', symbol='single'):
1982 def runsource(self, source, filename='<input>', symbol='single'):
1981 """Compile and run some source in the interpreter.
1983 """Compile and run some source in the interpreter.
1982
1984
1983 Arguments are as for compile_command().
1985 Arguments are as for compile_command().
1984
1986
1985 One several things can happen:
1987 One several things can happen:
1986
1988
1987 1) The input is incorrect; compile_command() raised an
1989 1) The input is incorrect; compile_command() raised an
1988 exception (SyntaxError or OverflowError). A syntax traceback
1990 exception (SyntaxError or OverflowError). A syntax traceback
1989 will be printed by calling the showsyntaxerror() method.
1991 will be printed by calling the showsyntaxerror() method.
1990
1992
1991 2) The input is incomplete, and more input is required;
1993 2) The input is incomplete, and more input is required;
1992 compile_command() returned None. Nothing happens.
1994 compile_command() returned None. Nothing happens.
1993
1995
1994 3) The input is complete; compile_command() returned a code
1996 3) The input is complete; compile_command() returned a code
1995 object. The code is executed by calling self.runcode() (which
1997 object. The code is executed by calling self.runcode() (which
1996 also handles run-time exceptions, except for SystemExit).
1998 also handles run-time exceptions, except for SystemExit).
1997
1999
1998 The return value is:
2000 The return value is:
1999
2001
2000 - True in case 2
2002 - True in case 2
2001
2003
2002 - False in the other cases, unless an exception is raised, where
2004 - False in the other cases, unless an exception is raised, where
2003 None is returned instead. This can be used by external callers to
2005 None is returned instead. This can be used by external callers to
2004 know whether to continue feeding input or not.
2006 know whether to continue feeding input or not.
2005
2007
2006 The return value can be used to decide whether to use sys.ps1 or
2008 The return value can be used to decide whether to use sys.ps1 or
2007 sys.ps2 to prompt the next line."""
2009 sys.ps2 to prompt the next line."""
2008
2010
2009 # if the source code has leading blanks, add 'if 1:\n' to it
2011 # if the source code has leading blanks, add 'if 1:\n' to it
2010 # this allows execution of indented pasted code. It is tempting
2012 # this allows execution of indented pasted code. It is tempting
2011 # to add '\n' at the end of source to run commands like ' a=1'
2013 # to add '\n' at the end of source to run commands like ' a=1'
2012 # directly, but this fails for more complicated scenarios
2014 # directly, but this fails for more complicated scenarios
2013 source=source.encode(self.stdin_encoding)
2015 source=source.encode(self.stdin_encoding)
2014 if source[:1] in [' ', '\t']:
2016 if source[:1] in [' ', '\t']:
2015 source = 'if 1:\n%s' % source
2017 source = 'if 1:\n%s' % source
2016
2018
2017 try:
2019 try:
2018 code = self.compile(source,filename,symbol)
2020 code = self.compile(source,filename,symbol)
2019 except (OverflowError, SyntaxError, ValueError, TypeError):
2021 except (OverflowError, SyntaxError, ValueError, TypeError):
2020 # Case 1
2022 # Case 1
2021 self.showsyntaxerror(filename)
2023 self.showsyntaxerror(filename)
2022 return None
2024 return None
2023
2025
2024 if code is None:
2026 if code is None:
2025 # Case 2
2027 # Case 2
2026 return True
2028 return True
2027
2029
2028 # Case 3
2030 # Case 3
2029 # We store the code object so that threaded shells and
2031 # We store the code object so that threaded shells and
2030 # custom exception handlers can access all this info if needed.
2032 # custom exception handlers can access all this info if needed.
2031 # The source corresponding to this can be obtained from the
2033 # The source corresponding to this can be obtained from the
2032 # buffer attribute as '\n'.join(self.buffer).
2034 # buffer attribute as '\n'.join(self.buffer).
2033 self.code_to_run = code
2035 self.code_to_run = code
2034 # now actually execute the code object
2036 # now actually execute the code object
2035 if self.runcode(code) == 0:
2037 if self.runcode(code) == 0:
2036 return False
2038 return False
2037 else:
2039 else:
2038 return None
2040 return None
2039
2041
2040 def runcode(self,code_obj):
2042 def runcode(self,code_obj):
2041 """Execute a code object.
2043 """Execute a code object.
2042
2044
2043 When an exception occurs, self.showtraceback() is called to display a
2045 When an exception occurs, self.showtraceback() is called to display a
2044 traceback.
2046 traceback.
2045
2047
2046 Return value: a flag indicating whether the code to be run completed
2048 Return value: a flag indicating whether the code to be run completed
2047 successfully:
2049 successfully:
2048
2050
2049 - 0: successful execution.
2051 - 0: successful execution.
2050 - 1: an error occurred.
2052 - 1: an error occurred.
2051 """
2053 """
2052
2054
2053 # Set our own excepthook in case the user code tries to call it
2055 # Set our own excepthook in case the user code tries to call it
2054 # directly, so that the IPython crash handler doesn't get triggered
2056 # directly, so that the IPython crash handler doesn't get triggered
2055 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2057 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2056
2058
2057 # we save the original sys.excepthook in the instance, in case config
2059 # we save the original sys.excepthook in the instance, in case config
2058 # code (such as magics) needs access to it.
2060 # code (such as magics) needs access to it.
2059 self.sys_excepthook = old_excepthook
2061 self.sys_excepthook = old_excepthook
2060 outflag = 1 # happens in more places, so it's easier as default
2062 outflag = 1 # happens in more places, so it's easier as default
2061 try:
2063 try:
2062 try:
2064 try:
2063 self.hooks.pre_runcode_hook()
2065 self.hooks.pre_runcode_hook()
2064 # Embedded instances require separate global/local namespaces
2066 # Embedded instances require separate global/local namespaces
2065 # so they can see both the surrounding (local) namespace and
2067 # so they can see both the surrounding (local) namespace and
2066 # the module-level globals when called inside another function.
2068 # the module-level globals when called inside another function.
2067 if self.embedded:
2069 if self.embedded:
2068 exec code_obj in self.user_global_ns, self.user_ns
2070 exec code_obj in self.user_global_ns, self.user_ns
2069 # Normal (non-embedded) instances should only have a single
2071 # Normal (non-embedded) instances should only have a single
2070 # namespace for user code execution, otherwise functions won't
2072 # namespace for user code execution, otherwise functions won't
2071 # see interactive top-level globals.
2073 # see interactive top-level globals.
2072 else:
2074 else:
2073 exec code_obj in self.user_ns
2075 exec code_obj in self.user_ns
2074 finally:
2076 finally:
2075 # Reset our crash handler in place
2077 # Reset our crash handler in place
2076 sys.excepthook = old_excepthook
2078 sys.excepthook = old_excepthook
2077 except SystemExit:
2079 except SystemExit:
2078 self.resetbuffer()
2080 self.resetbuffer()
2079 self.showtraceback()
2081 self.showtraceback()
2080 warn("Type %exit or %quit to exit IPython "
2082 warn("Type %exit or %quit to exit IPython "
2081 "(%Exit or %Quit do so unconditionally).",level=1)
2083 "(%Exit or %Quit do so unconditionally).",level=1)
2082 except self.custom_exceptions:
2084 except self.custom_exceptions:
2083 etype,value,tb = sys.exc_info()
2085 etype,value,tb = sys.exc_info()
2084 self.CustomTB(etype,value,tb)
2086 self.CustomTB(etype,value,tb)
2085 except:
2087 except:
2086 self.showtraceback()
2088 self.showtraceback()
2087 else:
2089 else:
2088 outflag = 0
2090 outflag = 0
2089 if softspace(sys.stdout, 0):
2091 if softspace(sys.stdout, 0):
2090 print
2092 print
2091 # Flush out code object which has been run (and source)
2093 # Flush out code object which has been run (and source)
2092 self.code_to_run = None
2094 self.code_to_run = None
2093 return outflag
2095 return outflag
2094
2096
2095 def push(self, line):
2097 def push(self, line):
2096 """Push a line to the interpreter.
2098 """Push a line to the interpreter.
2097
2099
2098 The line should not have a trailing newline; it may have
2100 The line should not have a trailing newline; it may have
2099 internal newlines. The line is appended to a buffer and the
2101 internal newlines. The line is appended to a buffer and the
2100 interpreter's runsource() method is called with the
2102 interpreter's runsource() method is called with the
2101 concatenated contents of the buffer as source. If this
2103 concatenated contents of the buffer as source. If this
2102 indicates that the command was executed or invalid, the buffer
2104 indicates that the command was executed or invalid, the buffer
2103 is reset; otherwise, the command is incomplete, and the buffer
2105 is reset; otherwise, the command is incomplete, and the buffer
2104 is left as it was after the line was appended. The return
2106 is left as it was after the line was appended. The return
2105 value is 1 if more input is required, 0 if the line was dealt
2107 value is 1 if more input is required, 0 if the line was dealt
2106 with in some way (this is the same as runsource()).
2108 with in some way (this is the same as runsource()).
2107 """
2109 """
2108
2110
2109 # autoindent management should be done here, and not in the
2111 # autoindent management should be done here, and not in the
2110 # interactive loop, since that one is only seen by keyboard input. We
2112 # interactive loop, since that one is only seen by keyboard input. We
2111 # need this done correctly even for code run via runlines (which uses
2113 # need this done correctly even for code run via runlines (which uses
2112 # push).
2114 # push).
2113
2115
2114 #print 'push line: <%s>' % line # dbg
2116 #print 'push line: <%s>' % line # dbg
2115 for subline in line.splitlines():
2117 for subline in line.splitlines():
2116 self.autoindent_update(subline)
2118 self.autoindent_update(subline)
2117 self.buffer.append(line)
2119 self.buffer.append(line)
2118 more = self.runsource('\n'.join(self.buffer), self.filename)
2120 more = self.runsource('\n'.join(self.buffer), self.filename)
2119 if not more:
2121 if not more:
2120 self.resetbuffer()
2122 self.resetbuffer()
2121 return more
2123 return more
2122
2124
2123 def split_user_input(self, line):
2125 def split_user_input(self, line):
2124 # This is really a hold-over to support ipapi and some extensions
2126 # This is really a hold-over to support ipapi and some extensions
2125 return prefilter.splitUserInput(line)
2127 return prefilter.splitUserInput(line)
2126
2128
2127 def resetbuffer(self):
2129 def resetbuffer(self):
2128 """Reset the input buffer."""
2130 """Reset the input buffer."""
2129 self.buffer[:] = []
2131 self.buffer[:] = []
2130
2132
2131 def raw_input(self,prompt='',continue_prompt=False):
2133 def raw_input(self,prompt='',continue_prompt=False):
2132 """Write a prompt and read a line.
2134 """Write a prompt and read a line.
2133
2135
2134 The returned line does not include the trailing newline.
2136 The returned line does not include the trailing newline.
2135 When the user enters the EOF key sequence, EOFError is raised.
2137 When the user enters the EOF key sequence, EOFError is raised.
2136
2138
2137 Optional inputs:
2139 Optional inputs:
2138
2140
2139 - prompt(''): a string to be printed to prompt the user.
2141 - prompt(''): a string to be printed to prompt the user.
2140
2142
2141 - continue_prompt(False): whether this line is the first one or a
2143 - continue_prompt(False): whether this line is the first one or a
2142 continuation in a sequence of inputs.
2144 continuation in a sequence of inputs.
2143 """
2145 """
2144
2146
2145 # Code run by the user may have modified the readline completer state.
2147 # Code run by the user may have modified the readline completer state.
2146 # We must ensure that our completer is back in place.
2148 # We must ensure that our completer is back in place.
2147 if self.has_readline:
2149 if self.has_readline:
2148 self.set_completer()
2150 self.set_completer()
2149
2151
2150 try:
2152 try:
2151 line = raw_input_original(prompt).decode(self.stdin_encoding)
2153 line = raw_input_original(prompt).decode(self.stdin_encoding)
2152 except ValueError:
2154 except ValueError:
2153 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2155 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2154 " or sys.stdout.close()!\nExiting IPython!")
2156 " or sys.stdout.close()!\nExiting IPython!")
2155 self.exit_now = True
2157 self.exit_now = True
2156 return ""
2158 return ""
2157
2159
2158 # Try to be reasonably smart about not re-indenting pasted input more
2160 # Try to be reasonably smart about not re-indenting pasted input more
2159 # than necessary. We do this by trimming out the auto-indent initial
2161 # than necessary. We do this by trimming out the auto-indent initial
2160 # spaces, if the user's actual input started itself with whitespace.
2162 # spaces, if the user's actual input started itself with whitespace.
2161 #debugx('self.buffer[-1]')
2163 #debugx('self.buffer[-1]')
2162
2164
2163 if self.autoindent:
2165 if self.autoindent:
2164 if num_ini_spaces(line) > self.indent_current_nsp:
2166 if num_ini_spaces(line) > self.indent_current_nsp:
2165 line = line[self.indent_current_nsp:]
2167 line = line[self.indent_current_nsp:]
2166 self.indent_current_nsp = 0
2168 self.indent_current_nsp = 0
2167
2169
2168 # store the unfiltered input before the user has any chance to modify
2170 # store the unfiltered input before the user has any chance to modify
2169 # it.
2171 # it.
2170 if line.strip():
2172 if line.strip():
2171 if continue_prompt:
2173 if continue_prompt:
2172 self.input_hist_raw[-1] += '%s\n' % line
2174 self.input_hist_raw[-1] += '%s\n' % line
2173 if self.has_readline: # and some config option is set?
2175 if self.has_readline: # and some config option is set?
2174 try:
2176 try:
2175 histlen = self.readline.get_current_history_length()
2177 histlen = self.readline.get_current_history_length()
2176 if histlen > 1:
2178 if histlen > 1:
2177 newhist = self.input_hist_raw[-1].rstrip()
2179 newhist = self.input_hist_raw[-1].rstrip()
2178 self.readline.remove_history_item(histlen-1)
2180 self.readline.remove_history_item(histlen-1)
2179 self.readline.replace_history_item(histlen-2,
2181 self.readline.replace_history_item(histlen-2,
2180 newhist.encode(self.stdin_encoding))
2182 newhist.encode(self.stdin_encoding))
2181 except AttributeError:
2183 except AttributeError:
2182 pass # re{move,place}_history_item are new in 2.4.
2184 pass # re{move,place}_history_item are new in 2.4.
2183 else:
2185 else:
2184 self.input_hist_raw.append('%s\n' % line)
2186 self.input_hist_raw.append('%s\n' % line)
2185 # only entries starting at first column go to shadow history
2187 # only entries starting at first column go to shadow history
2186 if line.lstrip() == line:
2188 if line.lstrip() == line:
2187 self.shadowhist.add(line.strip())
2189 self.shadowhist.add(line.strip())
2188 elif not continue_prompt:
2190 elif not continue_prompt:
2189 self.input_hist_raw.append('\n')
2191 self.input_hist_raw.append('\n')
2190 try:
2192 try:
2191 lineout = self.prefilter(line,continue_prompt)
2193 lineout = self.prefilter(line,continue_prompt)
2192 except:
2194 except:
2193 # blanket except, in case a user-defined prefilter crashes, so it
2195 # blanket except, in case a user-defined prefilter crashes, so it
2194 # can't take all of ipython with it.
2196 # can't take all of ipython with it.
2195 self.showtraceback()
2197 self.showtraceback()
2196 return ''
2198 return ''
2197 else:
2199 else:
2198 return lineout
2200 return lineout
2199
2201
2200 def _prefilter(self, line, continue_prompt):
2202 def _prefilter(self, line, continue_prompt):
2201 """Calls different preprocessors, depending on the form of line."""
2203 """Calls different preprocessors, depending on the form of line."""
2202
2204
2203 # All handlers *must* return a value, even if it's blank ('').
2205 # All handlers *must* return a value, even if it's blank ('').
2204
2206
2205 # Lines are NOT logged here. Handlers should process the line as
2207 # Lines are NOT logged here. Handlers should process the line as
2206 # needed, update the cache AND log it (so that the input cache array
2208 # needed, update the cache AND log it (so that the input cache array
2207 # stays synced).
2209 # stays synced).
2208
2210
2209 #.....................................................................
2211 #.....................................................................
2210 # Code begins
2212 # Code begins
2211
2213
2212 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
2214 #if line.startswith('%crash'): raise RuntimeError,'Crash now!' # dbg
2213
2215
2214 # save the line away in case we crash, so the post-mortem handler can
2216 # save the line away in case we crash, so the post-mortem handler can
2215 # record it
2217 # record it
2216 self._last_input_line = line
2218 self._last_input_line = line
2217
2219
2218 #print '***line: <%s>' % line # dbg
2220 #print '***line: <%s>' % line # dbg
2219
2221
2220 if not line:
2222 if not line:
2221 # Return immediately on purely empty lines, so that if the user
2223 # Return immediately on purely empty lines, so that if the user
2222 # previously typed some whitespace that started a continuation
2224 # previously typed some whitespace that started a continuation
2223 # prompt, he can break out of that loop with just an empty line.
2225 # prompt, he can break out of that loop with just an empty line.
2224 # This is how the default python prompt works.
2226 # This is how the default python prompt works.
2225
2227
2226 # Only return if the accumulated input buffer was just whitespace!
2228 # Only return if the accumulated input buffer was just whitespace!
2227 if ''.join(self.buffer).isspace():
2229 if ''.join(self.buffer).isspace():
2228 self.buffer[:] = []
2230 self.buffer[:] = []
2229 return ''
2231 return ''
2230
2232
2231 line_info = prefilter.LineInfo(line, continue_prompt)
2233 line_info = prefilter.LineInfo(line, continue_prompt)
2232
2234
2233 # the input history needs to track even empty lines
2235 # the input history needs to track even empty lines
2234 stripped = line.strip()
2236 stripped = line.strip()
2235
2237
2236 if not stripped:
2238 if not stripped:
2237 if not continue_prompt:
2239 if not continue_prompt:
2238 self.outputcache.prompt_count -= 1
2240 self.outputcache.prompt_count -= 1
2239 return self.handle_normal(line_info)
2241 return self.handle_normal(line_info)
2240
2242
2241 # print '***cont',continue_prompt # dbg
2243 # print '***cont',continue_prompt # dbg
2242 # special handlers are only allowed for single line statements
2244 # special handlers are only allowed for single line statements
2243 if continue_prompt and not self.rc.multi_line_specials:
2245 if continue_prompt and not self.rc.multi_line_specials:
2244 return self.handle_normal(line_info)
2246 return self.handle_normal(line_info)
2245
2247
2246
2248
2247 # See whether any pre-existing handler can take care of it
2249 # See whether any pre-existing handler can take care of it
2248 rewritten = self.hooks.input_prefilter(stripped)
2250 rewritten = self.hooks.input_prefilter(stripped)
2249 if rewritten != stripped: # ok, some prefilter did something
2251 if rewritten != stripped: # ok, some prefilter did something
2250 rewritten = line_info.pre + rewritten # add indentation
2252 rewritten = line_info.pre + rewritten # add indentation
2251 return self.handle_normal(prefilter.LineInfo(rewritten,
2253 return self.handle_normal(prefilter.LineInfo(rewritten,
2252 continue_prompt))
2254 continue_prompt))
2253
2255
2254 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2256 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2255
2257
2256 return prefilter.prefilter(line_info, self)
2258 return prefilter.prefilter(line_info, self)
2257
2259
2258
2260
2259 def _prefilter_dumb(self, line, continue_prompt):
2261 def _prefilter_dumb(self, line, continue_prompt):
2260 """simple prefilter function, for debugging"""
2262 """simple prefilter function, for debugging"""
2261 return self.handle_normal(line,continue_prompt)
2263 return self.handle_normal(line,continue_prompt)
2262
2264
2263
2265
2264 def multiline_prefilter(self, line, continue_prompt):
2266 def multiline_prefilter(self, line, continue_prompt):
2265 """ Run _prefilter for each line of input
2267 """ Run _prefilter for each line of input
2266
2268
2267 Covers cases where there are multiple lines in the user entry,
2269 Covers cases where there are multiple lines in the user entry,
2268 which is the case when the user goes back to a multiline history
2270 which is the case when the user goes back to a multiline history
2269 entry and presses enter.
2271 entry and presses enter.
2270
2272
2271 """
2273 """
2272 out = []
2274 out = []
2273 for l in line.rstrip('\n').split('\n'):
2275 for l in line.rstrip('\n').split('\n'):
2274 out.append(self._prefilter(l, continue_prompt))
2276 out.append(self._prefilter(l, continue_prompt))
2275 return '\n'.join(out)
2277 return '\n'.join(out)
2276
2278
2277 # Set the default prefilter() function (this can be user-overridden)
2279 # Set the default prefilter() function (this can be user-overridden)
2278 prefilter = multiline_prefilter
2280 prefilter = multiline_prefilter
2279
2281
2280 def handle_normal(self,line_info):
2282 def handle_normal(self,line_info):
2281 """Handle normal input lines. Use as a template for handlers."""
2283 """Handle normal input lines. Use as a template for handlers."""
2282
2284
2283 # With autoindent on, we need some way to exit the input loop, and I
2285 # With autoindent on, we need some way to exit the input loop, and I
2284 # don't want to force the user to have to backspace all the way to
2286 # don't want to force the user to have to backspace all the way to
2285 # clear the line. The rule will be in this case, that either two
2287 # clear the line. The rule will be in this case, that either two
2286 # lines of pure whitespace in a row, or a line of pure whitespace but
2288 # lines of pure whitespace in a row, or a line of pure whitespace but
2287 # of a size different to the indent level, will exit the input loop.
2289 # of a size different to the indent level, will exit the input loop.
2288 line = line_info.line
2290 line = line_info.line
2289 continue_prompt = line_info.continue_prompt
2291 continue_prompt = line_info.continue_prompt
2290
2292
2291 if (continue_prompt and self.autoindent and line.isspace() and
2293 if (continue_prompt and self.autoindent and line.isspace() and
2292 (0 < abs(len(line) - self.indent_current_nsp) <= 2 or
2294 (0 < abs(len(line) - self.indent_current_nsp) <= 2 or
2293 (self.buffer[-1]).isspace() )):
2295 (self.buffer[-1]).isspace() )):
2294 line = ''
2296 line = ''
2295
2297
2296 self.log(line,line,continue_prompt)
2298 self.log(line,line,continue_prompt)
2297 return line
2299 return line
2298
2300
2299 def handle_alias(self,line_info):
2301 def handle_alias(self,line_info):
2300 """Handle alias input lines. """
2302 """Handle alias input lines. """
2301 tgt = self.alias_table[line_info.iFun]
2303 tgt = self.alias_table[line_info.iFun]
2302 # print "=>",tgt #dbg
2304 # print "=>",tgt #dbg
2303 if callable(tgt):
2305 if callable(tgt):
2304 if '$' in line_info.line:
2306 if '$' in line_info.line:
2305 call_meth = '(_ip, _ip.itpl(%s))'
2307 call_meth = '(_ip, _ip.itpl(%s))'
2306 else:
2308 else:
2307 call_meth = '(_ip,%s)'
2309 call_meth = '(_ip,%s)'
2308 line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace,
2310 line_out = ("%s_sh.%s" + call_meth) % (line_info.preWhitespace,
2309 line_info.iFun,
2311 line_info.iFun,
2310 make_quoted_expr(line_info.line))
2312 make_quoted_expr(line_info.line))
2311 else:
2313 else:
2312 transformed = self.expand_aliases(line_info.iFun,line_info.theRest)
2314 transformed = self.expand_aliases(line_info.iFun,line_info.theRest)
2313
2315
2314 # pre is needed, because it carries the leading whitespace. Otherwise
2316 # pre is needed, because it carries the leading whitespace. Otherwise
2315 # aliases won't work in indented sections.
2317 # aliases won't work in indented sections.
2316 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2318 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2317 make_quoted_expr( transformed ))
2319 make_quoted_expr( transformed ))
2318
2320
2319 self.log(line_info.line,line_out,line_info.continue_prompt)
2321 self.log(line_info.line,line_out,line_info.continue_prompt)
2320 #print 'line out:',line_out # dbg
2322 #print 'line out:',line_out # dbg
2321 return line_out
2323 return line_out
2322
2324
2323 def handle_shell_escape(self, line_info):
2325 def handle_shell_escape(self, line_info):
2324 """Execute the line in a shell, empty return value"""
2326 """Execute the line in a shell, empty return value"""
2325 #print 'line in :', `line` # dbg
2327 #print 'line in :', `line` # dbg
2326 line = line_info.line
2328 line = line_info.line
2327 if line.lstrip().startswith('!!'):
2329 if line.lstrip().startswith('!!'):
2328 # rewrite LineInfo's line, iFun and theRest to properly hold the
2330 # rewrite LineInfo's line, iFun and theRest to properly hold the
2329 # call to %sx and the actual command to be executed, so
2331 # call to %sx and the actual command to be executed, so
2330 # handle_magic can work correctly. Note that this works even if
2332 # handle_magic can work correctly. Note that this works even if
2331 # the line is indented, so it handles multi_line_specials
2333 # the line is indented, so it handles multi_line_specials
2332 # properly.
2334 # properly.
2333 new_rest = line.lstrip()[2:]
2335 new_rest = line.lstrip()[2:]
2334 line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest)
2336 line_info.line = '%ssx %s' % (self.ESC_MAGIC,new_rest)
2335 line_info.iFun = 'sx'
2337 line_info.iFun = 'sx'
2336 line_info.theRest = new_rest
2338 line_info.theRest = new_rest
2337 return self.handle_magic(line_info)
2339 return self.handle_magic(line_info)
2338 else:
2340 else:
2339 cmd = line.lstrip().lstrip('!')
2341 cmd = line.lstrip().lstrip('!')
2340 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2342 line_out = '%s_ip.system(%s)' % (line_info.preWhitespace,
2341 make_quoted_expr(cmd))
2343 make_quoted_expr(cmd))
2342 # update cache/log and return
2344 # update cache/log and return
2343 self.log(line,line_out,line_info.continue_prompt)
2345 self.log(line,line_out,line_info.continue_prompt)
2344 return line_out
2346 return line_out
2345
2347
2346 def handle_magic(self, line_info):
2348 def handle_magic(self, line_info):
2347 """Execute magic functions."""
2349 """Execute magic functions."""
2348 iFun = line_info.iFun
2350 iFun = line_info.iFun
2349 theRest = line_info.theRest
2351 theRest = line_info.theRest
2350 cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace,
2352 cmd = '%s_ip.magic(%s)' % (line_info.preWhitespace,
2351 make_quoted_expr(iFun + " " + theRest))
2353 make_quoted_expr(iFun + " " + theRest))
2352 self.log(line_info.line,cmd,line_info.continue_prompt)
2354 self.log(line_info.line,cmd,line_info.continue_prompt)
2353 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
2355 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
2354 return cmd
2356 return cmd
2355
2357
2356 def handle_auto(self, line_info):
2358 def handle_auto(self, line_info):
2357 """Hande lines which can be auto-executed, quoting if requested."""
2359 """Hande lines which can be auto-executed, quoting if requested."""
2358
2360
2359 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2361 #print 'pre <%s> iFun <%s> rest <%s>' % (pre,iFun,theRest) # dbg
2360 line = line_info.line
2362 line = line_info.line
2361 iFun = line_info.iFun
2363 iFun = line_info.iFun
2362 theRest = line_info.theRest
2364 theRest = line_info.theRest
2363 pre = line_info.pre
2365 pre = line_info.pre
2364 continue_prompt = line_info.continue_prompt
2366 continue_prompt = line_info.continue_prompt
2365 obj = line_info.ofind(self)['obj']
2367 obj = line_info.ofind(self)['obj']
2366
2368
2367 # This should only be active for single-line input!
2369 # This should only be active for single-line input!
2368 if continue_prompt:
2370 if continue_prompt:
2369 self.log(line,line,continue_prompt)
2371 self.log(line,line,continue_prompt)
2370 return line
2372 return line
2371
2373
2372 force_auto = isinstance(obj, IPython.ipapi.IPyAutocall)
2374 force_auto = isinstance(obj, IPython.ipapi.IPyAutocall)
2373 auto_rewrite = True
2375 auto_rewrite = True
2374
2376
2375 if pre == self.ESC_QUOTE:
2377 if pre == self.ESC_QUOTE:
2376 # Auto-quote splitting on whitespace
2378 # Auto-quote splitting on whitespace
2377 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
2379 newcmd = '%s("%s")' % (iFun,'", "'.join(theRest.split()) )
2378 elif pre == self.ESC_QUOTE2:
2380 elif pre == self.ESC_QUOTE2:
2379 # Auto-quote whole string
2381 # Auto-quote whole string
2380 newcmd = '%s("%s")' % (iFun,theRest)
2382 newcmd = '%s("%s")' % (iFun,theRest)
2381 elif pre == self.ESC_PAREN:
2383 elif pre == self.ESC_PAREN:
2382 newcmd = '%s(%s)' % (iFun,",".join(theRest.split()))
2384 newcmd = '%s(%s)' % (iFun,",".join(theRest.split()))
2383 else:
2385 else:
2384 # Auto-paren.
2386 # Auto-paren.
2385 # We only apply it to argument-less calls if the autocall
2387 # We only apply it to argument-less calls if the autocall
2386 # parameter is set to 2. We only need to check that autocall is <
2388 # parameter is set to 2. We only need to check that autocall is <
2387 # 2, since this function isn't called unless it's at least 1.
2389 # 2, since this function isn't called unless it's at least 1.
2388 if not theRest and (self.rc.autocall < 2) and not force_auto:
2390 if not theRest and (self.rc.autocall < 2) and not force_auto:
2389 newcmd = '%s %s' % (iFun,theRest)
2391 newcmd = '%s %s' % (iFun,theRest)
2390 auto_rewrite = False
2392 auto_rewrite = False
2391 else:
2393 else:
2392 if not force_auto and theRest.startswith('['):
2394 if not force_auto and theRest.startswith('['):
2393 if hasattr(obj,'__getitem__'):
2395 if hasattr(obj,'__getitem__'):
2394 # Don't autocall in this case: item access for an object
2396 # Don't autocall in this case: item access for an object
2395 # which is BOTH callable and implements __getitem__.
2397 # which is BOTH callable and implements __getitem__.
2396 newcmd = '%s %s' % (iFun,theRest)
2398 newcmd = '%s %s' % (iFun,theRest)
2397 auto_rewrite = False
2399 auto_rewrite = False
2398 else:
2400 else:
2399 # if the object doesn't support [] access, go ahead and
2401 # if the object doesn't support [] access, go ahead and
2400 # autocall
2402 # autocall
2401 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2403 newcmd = '%s(%s)' % (iFun.rstrip(),theRest)
2402 elif theRest.endswith(';'):
2404 elif theRest.endswith(';'):
2403 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
2405 newcmd = '%s(%s);' % (iFun.rstrip(),theRest[:-1])
2404 else:
2406 else:
2405 newcmd = '%s(%s)' % (iFun.rstrip(), theRest)
2407 newcmd = '%s(%s)' % (iFun.rstrip(), theRest)
2406
2408
2407 if auto_rewrite:
2409 if auto_rewrite:
2408 rw = self.outputcache.prompt1.auto_rewrite() + newcmd
2410 rw = self.outputcache.prompt1.auto_rewrite() + newcmd
2409
2411
2410 try:
2412 try:
2411 # plain ascii works better w/ pyreadline, on some machines, so
2413 # plain ascii works better w/ pyreadline, on some machines, so
2412 # we use it and only print uncolored rewrite if we have unicode
2414 # we use it and only print uncolored rewrite if we have unicode
2413 rw = str(rw)
2415 rw = str(rw)
2414 print >>Term.cout, rw
2416 print >>Term.cout, rw
2415 except UnicodeEncodeError:
2417 except UnicodeEncodeError:
2416 print "-------------->" + newcmd
2418 print "-------------->" + newcmd
2417
2419
2418 # log what is now valid Python, not the actual user input (without the
2420 # log what is now valid Python, not the actual user input (without the
2419 # final newline)
2421 # final newline)
2420 self.log(line,newcmd,continue_prompt)
2422 self.log(line,newcmd,continue_prompt)
2421 return newcmd
2423 return newcmd
2422
2424
2423 def handle_help(self, line_info):
2425 def handle_help(self, line_info):
2424 """Try to get some help for the object.
2426 """Try to get some help for the object.
2425
2427
2426 obj? or ?obj -> basic information.
2428 obj? or ?obj -> basic information.
2427 obj?? or ??obj -> more details.
2429 obj?? or ??obj -> more details.
2428 """
2430 """
2429
2431
2430 line = line_info.line
2432 line = line_info.line
2431 # We need to make sure that we don't process lines which would be
2433 # We need to make sure that we don't process lines which would be
2432 # otherwise valid python, such as "x=1 # what?"
2434 # otherwise valid python, such as "x=1 # what?"
2433 try:
2435 try:
2434 codeop.compile_command(line)
2436 codeop.compile_command(line)
2435 except SyntaxError:
2437 except SyntaxError:
2436 # We should only handle as help stuff which is NOT valid syntax
2438 # We should only handle as help stuff which is NOT valid syntax
2437 if line[0]==self.ESC_HELP:
2439 if line[0]==self.ESC_HELP:
2438 line = line[1:]
2440 line = line[1:]
2439 elif line[-1]==self.ESC_HELP:
2441 elif line[-1]==self.ESC_HELP:
2440 line = line[:-1]
2442 line = line[:-1]
2441 self.log(line,'#?'+line,line_info.continue_prompt)
2443 self.log(line,'#?'+line,line_info.continue_prompt)
2442 if line:
2444 if line:
2443 #print 'line:<%r>' % line # dbg
2445 #print 'line:<%r>' % line # dbg
2444 self.magic_pinfo(line)
2446 self.magic_pinfo(line)
2445 else:
2447 else:
2446 page(self.usage,screen_lines=self.rc.screen_length)
2448 page(self.usage,screen_lines=self.rc.screen_length)
2447 return '' # Empty string is needed here!
2449 return '' # Empty string is needed here!
2448 except:
2450 except:
2449 # Pass any other exceptions through to the normal handler
2451 # Pass any other exceptions through to the normal handler
2450 return self.handle_normal(line_info)
2452 return self.handle_normal(line_info)
2451 else:
2453 else:
2452 # If the code compiles ok, we should handle it normally
2454 # If the code compiles ok, we should handle it normally
2453 return self.handle_normal(line_info)
2455 return self.handle_normal(line_info)
2454
2456
2455 def getapi(self):
2457 def getapi(self):
2456 """ Get an IPApi object for this shell instance
2458 """ Get an IPApi object for this shell instance
2457
2459
2458 Getting an IPApi object is always preferable to accessing the shell
2460 Getting an IPApi object is always preferable to accessing the shell
2459 directly, but this holds true especially for extensions.
2461 directly, but this holds true especially for extensions.
2460
2462
2461 It should always be possible to implement an extension with IPApi
2463 It should always be possible to implement an extension with IPApi
2462 alone. If not, contact maintainer to request an addition.
2464 alone. If not, contact maintainer to request an addition.
2463
2465
2464 """
2466 """
2465 return self.api
2467 return self.api
2466
2468
2467 def handle_emacs(self, line_info):
2469 def handle_emacs(self, line_info):
2468 """Handle input lines marked by python-mode."""
2470 """Handle input lines marked by python-mode."""
2469
2471
2470 # Currently, nothing is done. Later more functionality can be added
2472 # Currently, nothing is done. Later more functionality can be added
2471 # here if needed.
2473 # here if needed.
2472
2474
2473 # The input cache shouldn't be updated
2475 # The input cache shouldn't be updated
2474 return line_info.line
2476 return line_info.line
2475
2477
2476
2478
2477 def mktempfile(self,data=None):
2479 def mktempfile(self,data=None):
2478 """Make a new tempfile and return its filename.
2480 """Make a new tempfile and return its filename.
2479
2481
2480 This makes a call to tempfile.mktemp, but it registers the created
2482 This makes a call to tempfile.mktemp, but it registers the created
2481 filename internally so ipython cleans it up at exit time.
2483 filename internally so ipython cleans it up at exit time.
2482
2484
2483 Optional inputs:
2485 Optional inputs:
2484
2486
2485 - data(None): if data is given, it gets written out to the temp file
2487 - data(None): if data is given, it gets written out to the temp file
2486 immediately, and the file is closed again."""
2488 immediately, and the file is closed again."""
2487
2489
2488 filename = tempfile.mktemp('.py','ipython_edit_')
2490 filename = tempfile.mktemp('.py','ipython_edit_')
2489 self.tempfiles.append(filename)
2491 self.tempfiles.append(filename)
2490
2492
2491 if data:
2493 if data:
2492 tmp_file = open(filename,'w')
2494 tmp_file = open(filename,'w')
2493 tmp_file.write(data)
2495 tmp_file.write(data)
2494 tmp_file.close()
2496 tmp_file.close()
2495 return filename
2497 return filename
2496
2498
2497 def write(self,data):
2499 def write(self,data):
2498 """Write a string to the default output"""
2500 """Write a string to the default output"""
2499 Term.cout.write(data)
2501 Term.cout.write(data)
2500
2502
2501 def write_err(self,data):
2503 def write_err(self,data):
2502 """Write a string to the default error output"""
2504 """Write a string to the default error output"""
2503 Term.cerr.write(data)
2505 Term.cerr.write(data)
2504
2506
2505 def exit(self):
2507 def exit(self):
2506 """Handle interactive exit.
2508 """Handle interactive exit.
2507
2509
2508 This method sets the exit_now attribute."""
2510 This method sets the exit_now attribute."""
2509
2511
2510 if self.rc.confirm_exit:
2512 if self.rc.confirm_exit:
2511 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2513 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2512 self.exit_now = True
2514 self.exit_now = True
2513 else:
2515 else:
2514 self.exit_now = True
2516 self.exit_now = True
2515
2517
2516 def safe_execfile(self,fname,*where,**kw):
2518 def safe_execfile(self,fname,*where,**kw):
2517 """A safe version of the builtin execfile().
2519 """A safe version of the builtin execfile().
2518
2520
2519 This version will never throw an exception, and knows how to handle
2521 This version will never throw an exception, and knows how to handle
2520 ipython logs as well.
2522 ipython logs as well.
2521
2523
2522 :Parameters:
2524 :Parameters:
2523 fname : string
2525 fname : string
2524 Name of the file to be executed.
2526 Name of the file to be executed.
2525
2527
2526 where : tuple
2528 where : tuple
2527 One or two namespaces, passed to execfile() as (globals,locals).
2529 One or two namespaces, passed to execfile() as (globals,locals).
2528 If only one is given, it is passed as both.
2530 If only one is given, it is passed as both.
2529
2531
2530 :Keywords:
2532 :Keywords:
2531 islog : boolean (False)
2533 islog : boolean (False)
2532
2534
2533 quiet : boolean (True)
2535 quiet : boolean (True)
2534
2536
2535 exit_ignore : boolean (False)
2537 exit_ignore : boolean (False)
2536 """
2538 """
2537
2539
2538 def syspath_cleanup():
2540 def syspath_cleanup():
2539 """Internal cleanup routine for sys.path."""
2541 """Internal cleanup routine for sys.path."""
2540 if add_dname:
2542 if add_dname:
2541 try:
2543 try:
2542 sys.path.remove(dname)
2544 sys.path.remove(dname)
2543 except ValueError:
2545 except ValueError:
2544 # For some reason the user has already removed it, ignore.
2546 # For some reason the user has already removed it, ignore.
2545 pass
2547 pass
2546
2548
2547 fname = os.path.expanduser(fname)
2549 fname = os.path.expanduser(fname)
2548
2550
2549 # Find things also in current directory. This is needed to mimic the
2551 # Find things also in current directory. This is needed to mimic the
2550 # behavior of running a script from the system command line, where
2552 # behavior of running a script from the system command line, where
2551 # Python inserts the script's directory into sys.path
2553 # Python inserts the script's directory into sys.path
2552 dname = os.path.dirname(os.path.abspath(fname))
2554 dname = os.path.dirname(os.path.abspath(fname))
2553 add_dname = False
2555 add_dname = False
2554 if dname not in sys.path:
2556 if dname not in sys.path:
2555 sys.path.insert(0,dname)
2557 sys.path.insert(0,dname)
2556 add_dname = True
2558 add_dname = True
2557
2559
2558 try:
2560 try:
2559 xfile = open(fname)
2561 xfile = open(fname)
2560 except:
2562 except:
2561 print >> Term.cerr, \
2563 print >> Term.cerr, \
2562 'Could not open file <%s> for safe execution.' % fname
2564 'Could not open file <%s> for safe execution.' % fname
2563 syspath_cleanup()
2565 syspath_cleanup()
2564 return None
2566 return None
2565
2567
2566 kw.setdefault('islog',0)
2568 kw.setdefault('islog',0)
2567 kw.setdefault('quiet',1)
2569 kw.setdefault('quiet',1)
2568 kw.setdefault('exit_ignore',0)
2570 kw.setdefault('exit_ignore',0)
2569
2571
2570 first = xfile.readline()
2572 first = xfile.readline()
2571 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2573 loghead = str(self.loghead_tpl).split('\n',1)[0].strip()
2572 xfile.close()
2574 xfile.close()
2573 # line by line execution
2575 # line by line execution
2574 if first.startswith(loghead) or kw['islog']:
2576 if first.startswith(loghead) or kw['islog']:
2575 print 'Loading log file <%s> one line at a time...' % fname
2577 print 'Loading log file <%s> one line at a time...' % fname
2576 if kw['quiet']:
2578 if kw['quiet']:
2577 stdout_save = sys.stdout
2579 stdout_save = sys.stdout
2578 sys.stdout = StringIO.StringIO()
2580 sys.stdout = StringIO.StringIO()
2579 try:
2581 try:
2580 globs,locs = where[0:2]
2582 globs,locs = where[0:2]
2581 except:
2583 except:
2582 try:
2584 try:
2583 globs = locs = where[0]
2585 globs = locs = where[0]
2584 except:
2586 except:
2585 globs = locs = globals()
2587 globs = locs = globals()
2586 badblocks = []
2588 badblocks = []
2587
2589
2588 # we also need to identify indented blocks of code when replaying
2590 # we also need to identify indented blocks of code when replaying
2589 # logs and put them together before passing them to an exec
2591 # logs and put them together before passing them to an exec
2590 # statement. This takes a bit of regexp and look-ahead work in the
2592 # statement. This takes a bit of regexp and look-ahead work in the
2591 # file. It's easiest if we swallow the whole thing in memory
2593 # file. It's easiest if we swallow the whole thing in memory
2592 # first, and manually walk through the lines list moving the
2594 # first, and manually walk through the lines list moving the
2593 # counter ourselves.
2595 # counter ourselves.
2594 indent_re = re.compile('\s+\S')
2596 indent_re = re.compile('\s+\S')
2595 xfile = open(fname)
2597 xfile = open(fname)
2596 filelines = xfile.readlines()
2598 filelines = xfile.readlines()
2597 xfile.close()
2599 xfile.close()
2598 nlines = len(filelines)
2600 nlines = len(filelines)
2599 lnum = 0
2601 lnum = 0
2600 while lnum < nlines:
2602 while lnum < nlines:
2601 line = filelines[lnum]
2603 line = filelines[lnum]
2602 lnum += 1
2604 lnum += 1
2603 # don't re-insert logger status info into cache
2605 # don't re-insert logger status info into cache
2604 if line.startswith('#log#'):
2606 if line.startswith('#log#'):
2605 continue
2607 continue
2606 else:
2608 else:
2607 # build a block of code (maybe a single line) for execution
2609 # build a block of code (maybe a single line) for execution
2608 block = line
2610 block = line
2609 try:
2611 try:
2610 next = filelines[lnum] # lnum has already incremented
2612 next = filelines[lnum] # lnum has already incremented
2611 except:
2613 except:
2612 next = None
2614 next = None
2613 while next and indent_re.match(next):
2615 while next and indent_re.match(next):
2614 block += next
2616 block += next
2615 lnum += 1
2617 lnum += 1
2616 try:
2618 try:
2617 next = filelines[lnum]
2619 next = filelines[lnum]
2618 except:
2620 except:
2619 next = None
2621 next = None
2620 # now execute the block of one or more lines
2622 # now execute the block of one or more lines
2621 try:
2623 try:
2622 exec block in globs,locs
2624 exec block in globs,locs
2623 except SystemExit:
2625 except SystemExit:
2624 pass
2626 pass
2625 except:
2627 except:
2626 badblocks.append(block.rstrip())
2628 badblocks.append(block.rstrip())
2627 if kw['quiet']: # restore stdout
2629 if kw['quiet']: # restore stdout
2628 sys.stdout.close()
2630 sys.stdout.close()
2629 sys.stdout = stdout_save
2631 sys.stdout = stdout_save
2630 print 'Finished replaying log file <%s>' % fname
2632 print 'Finished replaying log file <%s>' % fname
2631 if badblocks:
2633 if badblocks:
2632 print >> sys.stderr, ('\nThe following lines/blocks in file '
2634 print >> sys.stderr, ('\nThe following lines/blocks in file '
2633 '<%s> reported errors:' % fname)
2635 '<%s> reported errors:' % fname)
2634
2636
2635 for badline in badblocks:
2637 for badline in badblocks:
2636 print >> sys.stderr, badline
2638 print >> sys.stderr, badline
2637 else: # regular file execution
2639 else: # regular file execution
2638 try:
2640 try:
2639 if sys.platform == 'win32' and sys.version_info < (2,5,1):
2641 if sys.platform == 'win32' and sys.version_info < (2,5,1):
2640 # Work around a bug in Python for Windows. The bug was
2642 # Work around a bug in Python for Windows. The bug was
2641 # fixed in in Python 2.5 r54159 and 54158, but that's still
2643 # fixed in in Python 2.5 r54159 and 54158, but that's still
2642 # SVN Python as of March/07. For details, see:
2644 # SVN Python as of March/07. For details, see:
2643 # http://projects.scipy.org/ipython/ipython/ticket/123
2645 # http://projects.scipy.org/ipython/ipython/ticket/123
2644 try:
2646 try:
2645 globs,locs = where[0:2]
2647 globs,locs = where[0:2]
2646 except:
2648 except:
2647 try:
2649 try:
2648 globs = locs = where[0]
2650 globs = locs = where[0]
2649 except:
2651 except:
2650 globs = locs = globals()
2652 globs = locs = globals()
2651 exec file(fname) in globs,locs
2653 exec file(fname) in globs,locs
2652 else:
2654 else:
2653 execfile(fname,*where)
2655 execfile(fname,*where)
2654 except SyntaxError:
2656 except SyntaxError:
2655 self.showsyntaxerror()
2657 self.showsyntaxerror()
2656 warn('Failure executing file: <%s>' % fname)
2658 warn('Failure executing file: <%s>' % fname)
2657 except SystemExit,status:
2659 except SystemExit,status:
2658 # Code that correctly sets the exit status flag to success (0)
2660 # Code that correctly sets the exit status flag to success (0)
2659 # shouldn't be bothered with a traceback. Note that a plain
2661 # shouldn't be bothered with a traceback. Note that a plain
2660 # sys.exit() does NOT set the message to 0 (it's empty) so that
2662 # sys.exit() does NOT set the message to 0 (it's empty) so that
2661 # will still get a traceback. Note that the structure of the
2663 # will still get a traceback. Note that the structure of the
2662 # SystemExit exception changed between Python 2.4 and 2.5, so
2664 # SystemExit exception changed between Python 2.4 and 2.5, so
2663 # the checks must be done in a version-dependent way.
2665 # the checks must be done in a version-dependent way.
2664 show = False
2666 show = False
2665
2667
2666 if sys.version_info[:2] > (2,5):
2668 if sys.version_info[:2] > (2,5):
2667 if status.message!=0 and not kw['exit_ignore']:
2669 if status.message!=0 and not kw['exit_ignore']:
2668 show = True
2670 show = True
2669 else:
2671 else:
2670 if status.code and not kw['exit_ignore']:
2672 if status.code and not kw['exit_ignore']:
2671 show = True
2673 show = True
2672 if show:
2674 if show:
2673 self.showtraceback()
2675 self.showtraceback()
2674 warn('Failure executing file: <%s>' % fname)
2676 warn('Failure executing file: <%s>' % fname)
2675 except:
2677 except:
2676 self.showtraceback()
2678 self.showtraceback()
2677 warn('Failure executing file: <%s>' % fname)
2679 warn('Failure executing file: <%s>' % fname)
2678
2680
2679 syspath_cleanup()
2681 syspath_cleanup()
2680
2682
2681 #************************* end of file <iplib.py> *****************************
2683 #************************* end of file <iplib.py> *****************************
General Comments 0
You need to be logged in to leave comments. Login now