##// END OF EJS Templates
Move crash handling to the application level and simplify class structure....
Fernando Perez -
Show More
@@ -1,370 +1,373 b''
1 # coding: utf-8
1 # coding: utf-8
2 """A simple configuration system.
2 """A simple configuration system.
3
3
4 Authors
4 Authors
5 -------
5 -------
6 * Brian Granger
6 * Brian Granger
7 * Fernando Perez
7 * Fernando Perez
8 """
8 """
9
9
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Copyright (C) 2008-2009 The IPython Development Team
11 # Copyright (C) 2008-2009 The IPython Development Team
12 #
12 #
13 # Distributed under the terms of the BSD License. The full license is in
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
14 # the file COPYING, distributed as part of this software.
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18 # Imports
18 # Imports
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 import __builtin__
21 import __builtin__
22 import os
22 import os
23 import sys
23 import sys
24
24
25 from IPython.external import argparse
25 from IPython.external import argparse
26 from IPython.utils.genutils import filefind
26 from IPython.utils.genutils import filefind
27
27
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 # Exceptions
29 # Exceptions
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31
31
32
32
33 class ConfigError(Exception):
33 class ConfigError(Exception):
34 pass
34 pass
35
35
36
36
37 class ConfigLoaderError(ConfigError):
37 class ConfigLoaderError(ConfigError):
38 pass
38 pass
39
39
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41 # Argparse fix
41 # Argparse fix
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43 # Unfortunately argparse by default prints help messages to stderr instead of
43 # Unfortunately argparse by default prints help messages to stderr instead of
44 # stdout. This makes it annoying to capture long help screens at the command
44 # stdout. This makes it annoying to capture long help screens at the command
45 # line, since one must know how to pipe stderr, which many users don't know how
45 # line, since one must know how to pipe stderr, which many users don't know how
46 # to do. So we override the print_help method with one that defaults to
46 # to do. So we override the print_help method with one that defaults to
47 # stdout and use our class instead.
47 # stdout and use our class instead.
48
48
49 class ArgumentParser(argparse.ArgumentParser):
49 class ArgumentParser(argparse.ArgumentParser):
50 """Simple argparse subclass that prints help to stdout by default."""
50 """Simple argparse subclass that prints help to stdout by default."""
51
51
52 def print_help(self, file=None):
52 def print_help(self, file=None):
53 if file is None:
53 if file is None:
54 file = sys.stdout
54 file = sys.stdout
55 return super(ArgumentParser, self).print_help(file)
55 return super(ArgumentParser, self).print_help(file)
56
56
57 print_help.__doc__ = argparse.ArgumentParser.print_help.__doc__
57 print_help.__doc__ = argparse.ArgumentParser.print_help.__doc__
58
58
59 #-----------------------------------------------------------------------------
59 #-----------------------------------------------------------------------------
60 # Config class for holding config information
60 # Config class for holding config information
61 #-----------------------------------------------------------------------------
61 #-----------------------------------------------------------------------------
62
62
63
63
64 class Config(dict):
64 class Config(dict):
65 """An attribute based dict that can do smart merges."""
65 """An attribute based dict that can do smart merges."""
66
66
67 def __init__(self, *args, **kwds):
67 def __init__(self, *args, **kwds):
68 dict.__init__(self, *args, **kwds)
68 dict.__init__(self, *args, **kwds)
69 # This sets self.__dict__ = self, but it has to be done this way
69 # This sets self.__dict__ = self, but it has to be done this way
70 # because we are also overriding __setattr__.
70 # because we are also overriding __setattr__.
71 dict.__setattr__(self, '__dict__', self)
71 dict.__setattr__(self, '__dict__', self)
72
72
73 def _merge(self, other):
73 def _merge(self, other):
74 to_update = {}
74 to_update = {}
75 for k, v in other.items():
75 for k, v in other.items():
76 if not self.has_key(k):
76 if not self.has_key(k):
77 to_update[k] = v
77 to_update[k] = v
78 else: # I have this key
78 else: # I have this key
79 if isinstance(v, Config):
79 if isinstance(v, Config):
80 # Recursively merge common sub Configs
80 # Recursively merge common sub Configs
81 self[k]._merge(v)
81 self[k]._merge(v)
82 else:
82 else:
83 # Plain updates for non-Configs
83 # Plain updates for non-Configs
84 to_update[k] = v
84 to_update[k] = v
85
85
86 self.update(to_update)
86 self.update(to_update)
87
87
88 def _is_section_key(self, key):
88 def _is_section_key(self, key):
89 if key[0].upper()==key[0] and not key.startswith('_'):
89 if key[0].upper()==key[0] and not key.startswith('_'):
90 return True
90 return True
91 else:
91 else:
92 return False
92 return False
93
93
94 def has_key(self, key):
94 def has_key(self, key):
95 if self._is_section_key(key):
95 if self._is_section_key(key):
96 return True
96 return True
97 else:
97 else:
98 return dict.has_key(self, key)
98 return dict.has_key(self, key)
99
99
100 def _has_section(self, key):
100 def _has_section(self, key):
101 if self._is_section_key(key):
101 if self._is_section_key(key):
102 if dict.has_key(self, key):
102 if dict.has_key(self, key):
103 return True
103 return True
104 return False
104 return False
105
105
106 def copy(self):
106 def copy(self):
107 return type(self)(dict.copy(self))
107 return type(self)(dict.copy(self))
108
108
109 def __copy__(self):
109 def __copy__(self):
110 return self.copy()
110 return self.copy()
111
111
112 def __deepcopy__(self, memo):
112 def __deepcopy__(self, memo):
113 import copy
113 import copy
114 return type(self)(copy.deepcopy(self.items()))
114 return type(self)(copy.deepcopy(self.items()))
115
115
116 def __getitem__(self, key):
116 def __getitem__(self, key):
117 # Because we use this for an exec namespace, we need to delegate
117 # Because we use this for an exec namespace, we need to delegate
118 # the lookup of names in __builtin__ to itself. This means
118 # the lookup of names in __builtin__ to itself. This means
119 # that you can't have section or attribute names that are
119 # that you can't have section or attribute names that are
120 # builtins.
120 # builtins.
121 try:
121 try:
122 return getattr(__builtin__, key)
122 return getattr(__builtin__, key)
123 except AttributeError:
123 except AttributeError:
124 pass
124 pass
125 if self._is_section_key(key):
125 if self._is_section_key(key):
126 try:
126 try:
127 return dict.__getitem__(self, key)
127 return dict.__getitem__(self, key)
128 except KeyError:
128 except KeyError:
129 c = Config()
129 c = Config()
130 dict.__setitem__(self, key, c)
130 dict.__setitem__(self, key, c)
131 return c
131 return c
132 else:
132 else:
133 return dict.__getitem__(self, key)
133 return dict.__getitem__(self, key)
134
134
135 def __setitem__(self, key, value):
135 def __setitem__(self, key, value):
136 # Don't allow names in __builtin__ to be modified.
136 # Don't allow names in __builtin__ to be modified.
137 if hasattr(__builtin__, key):
137 if hasattr(__builtin__, key):
138 raise ConfigError('Config variable names cannot have the same name '
138 raise ConfigError('Config variable names cannot have the same name '
139 'as a Python builtin: %s' % key)
139 'as a Python builtin: %s' % key)
140 if self._is_section_key(key):
140 if self._is_section_key(key):
141 if not isinstance(value, Config):
141 if not isinstance(value, Config):
142 raise ValueError('values whose keys begin with an uppercase '
142 raise ValueError('values whose keys begin with an uppercase '
143 'char must be Config instances: %r, %r' % (key, value))
143 'char must be Config instances: %r, %r' % (key, value))
144 else:
144 else:
145 dict.__setitem__(self, key, value)
145 dict.__setitem__(self, key, value)
146
146
147 def __getattr__(self, key):
147 def __getattr__(self, key):
148 try:
148 try:
149 return self.__getitem__(key)
149 return self.__getitem__(key)
150 except KeyError, e:
150 except KeyError, e:
151 raise AttributeError(e)
151 raise AttributeError(e)
152
152
153 def __setattr__(self, key, value):
153 def __setattr__(self, key, value):
154 try:
154 try:
155 self.__setitem__(key, value)
155 self.__setitem__(key, value)
156 except KeyError, e:
156 except KeyError, e:
157 raise AttributeError(e)
157 raise AttributeError(e)
158
158
159 def __delattr__(self, key):
159 def __delattr__(self, key):
160 try:
160 try:
161 dict.__delitem__(self, key)
161 dict.__delitem__(self, key)
162 except KeyError, e:
162 except KeyError, e:
163 raise AttributeError(e)
163 raise AttributeError(e)
164
164
165
165
166 #-----------------------------------------------------------------------------
166 #-----------------------------------------------------------------------------
167 # Config loading classes
167 # Config loading classes
168 #-----------------------------------------------------------------------------
168 #-----------------------------------------------------------------------------
169
169
170
170
171 class ConfigLoader(object):
171 class ConfigLoader(object):
172 """A object for loading configurations from just about anywhere.
172 """A object for loading configurations from just about anywhere.
173
173
174 The resulting configuration is packaged as a :class:`Struct`.
174 The resulting configuration is packaged as a :class:`Struct`.
175
175
176 Notes
176 Notes
177 -----
177 -----
178 A :class:`ConfigLoader` does one thing: load a config from a source
178 A :class:`ConfigLoader` does one thing: load a config from a source
179 (file, command line arguments) and returns the data as a :class:`Struct`.
179 (file, command line arguments) and returns the data as a :class:`Struct`.
180 There are lots of things that :class:`ConfigLoader` does not do. It does
180 There are lots of things that :class:`ConfigLoader` does not do. It does
181 not implement complex logic for finding config files. It does not handle
181 not implement complex logic for finding config files. It does not handle
182 default values or merge multiple configs. These things need to be
182 default values or merge multiple configs. These things need to be
183 handled elsewhere.
183 handled elsewhere.
184 """
184 """
185
185
186 def __init__(self):
186 def __init__(self):
187 """A base class for config loaders.
187 """A base class for config loaders.
188
188
189 Examples
189 Examples
190 --------
190 --------
191
191
192 >>> cl = ConfigLoader()
192 >>> cl = ConfigLoader()
193 >>> config = cl.load_config()
193 >>> config = cl.load_config()
194 >>> config
194 >>> config
195 {}
195 {}
196 """
196 """
197 self.clear()
197 self.clear()
198
198
199 def clear(self):
199 def clear(self):
200 self.config = Config()
200 self.config = Config()
201
201
202 def load_config(self):
202 def load_config(self):
203 """Load a config from somewhere, return a Struct.
203 """Load a config from somewhere, return a Struct.
204
204
205 Usually, this will cause self.config to be set and then returned.
205 Usually, this will cause self.config to be set and then returned.
206 """
206 """
207 return self.config
207 return self.config
208
208
209
209
210 class FileConfigLoader(ConfigLoader):
210 class FileConfigLoader(ConfigLoader):
211 """A base class for file based configurations.
211 """A base class for file based configurations.
212
212
213 As we add more file based config loaders, the common logic should go
213 As we add more file based config loaders, the common logic should go
214 here.
214 here.
215 """
215 """
216 pass
216 pass
217
217
218
218
219 class PyFileConfigLoader(FileConfigLoader):
219 class PyFileConfigLoader(FileConfigLoader):
220 """A config loader for pure python files.
220 """A config loader for pure python files.
221
221
222 This calls execfile on a plain python file and looks for attributes
222 This calls execfile on a plain python file and looks for attributes
223 that are all caps. These attribute are added to the config Struct.
223 that are all caps. These attribute are added to the config Struct.
224 """
224 """
225
225
226 def __init__(self, filename, path=None):
226 def __init__(self, filename, path=None):
227 """Build a config loader for a filename and path.
227 """Build a config loader for a filename and path.
228
228
229 Parameters
229 Parameters
230 ----------
230 ----------
231 filename : str
231 filename : str
232 The file name of the config file.
232 The file name of the config file.
233 path : str, list, tuple
233 path : str, list, tuple
234 The path to search for the config file on, or a sequence of
234 The path to search for the config file on, or a sequence of
235 paths to try in order.
235 paths to try in order.
236 """
236 """
237 super(PyFileConfigLoader, self).__init__()
237 super(PyFileConfigLoader, self).__init__()
238 self.filename = filename
238 self.filename = filename
239 self.path = path
239 self.path = path
240 self.full_filename = ''
240 self.full_filename = ''
241 self.data = None
241 self.data = None
242
242
243 def load_config(self):
243 def load_config(self):
244 """Load the config from a file and return it as a Struct."""
244 """Load the config from a file and return it as a Struct."""
245 self._find_file()
245 self._find_file()
246 self._read_file_as_dict()
246 self._read_file_as_dict()
247 self._convert_to_config()
247 self._convert_to_config()
248 return self.config
248 return self.config
249
249
250 def _find_file(self):
250 def _find_file(self):
251 """Try to find the file by searching the paths."""
251 """Try to find the file by searching the paths."""
252 self.full_filename = filefind(self.filename, self.path)
252 self.full_filename = filefind(self.filename, self.path)
253
253
254 def _read_file_as_dict(self):
254 def _read_file_as_dict(self):
255 """Load the config file into self.config, with recursive loading."""
255 """Load the config file into self.config, with recursive loading."""
256 # This closure is made available in the namespace that is used
256 # This closure is made available in the namespace that is used
257 # to exec the config file. This allows users to call
257 # to exec the config file. This allows users to call
258 # load_subconfig('myconfig.py') to load config files recursively.
258 # load_subconfig('myconfig.py') to load config files recursively.
259 # It needs to be a closure because it has references to self.path
259 # It needs to be a closure because it has references to self.path
260 # and self.config. The sub-config is loaded with the same path
260 # and self.config. The sub-config is loaded with the same path
261 # as the parent, but it uses an empty config which is then merged
261 # as the parent, but it uses an empty config which is then merged
262 # with the parents.
262 # with the parents.
263 def load_subconfig(fname):
263 def load_subconfig(fname):
264 loader = PyFileConfigLoader(fname, self.path)
264 loader = PyFileConfigLoader(fname, self.path)
265 try:
265 try:
266 sub_config = loader.load_config()
266 sub_config = loader.load_config()
267 except IOError:
267 except IOError:
268 # Pass silently if the sub config is not there. This happens
268 # Pass silently if the sub config is not there. This happens
269 # when a user us using a profile, but not the default config.
269 # when a user us using a profile, but not the default config.
270 pass
270 pass
271 else:
271 else:
272 self.config._merge(sub_config)
272 self.config._merge(sub_config)
273
273
274 # Again, this needs to be a closure and should be used in config
274 # Again, this needs to be a closure and should be used in config
275 # files to get the config being loaded.
275 # files to get the config being loaded.
276 def get_config():
276 def get_config():
277 return self.config
277 return self.config
278
278
279 namespace = dict(load_subconfig=load_subconfig, get_config=get_config)
279 namespace = dict(load_subconfig=load_subconfig, get_config=get_config)
280 execfile(self.full_filename, namespace)
280 execfile(self.full_filename, namespace)
281
281
282 def _convert_to_config(self):
282 def _convert_to_config(self):
283 if self.data is None:
283 if self.data is None:
284 ConfigLoaderError('self.data does not exist')
284 ConfigLoaderError('self.data does not exist')
285
285
286
286
287 class CommandLineConfigLoader(ConfigLoader):
287 class CommandLineConfigLoader(ConfigLoader):
288 """A config loader for command line arguments.
288 """A config loader for command line arguments.
289
289
290 As we add more command line based loaders, the common logic should go
290 As we add more command line based loaders, the common logic should go
291 here.
291 here.
292 """
292 """
293
293
294
294
295 class NoConfigDefault(object): pass
295 class NoConfigDefault(object): pass
296 NoConfigDefault = NoConfigDefault()
296 NoConfigDefault = NoConfigDefault()
297
297
298
298
299 class ArgParseConfigLoader(CommandLineConfigLoader):
299 class ArgParseConfigLoader(CommandLineConfigLoader):
300
300
301 # arguments = [(('-f','--file'),dict(type=str,dest='file'))]
301 def __init__(self, argv=None, arguments=(), *args, **kw):
302 arguments = ()
303
304 def __init__(self, argv=None, *args, **kw):
305 """Create a config loader for use with argparse.
302 """Create a config loader for use with argparse.
306
303
307 With the exception of argv, other args and kwargs arguments here are
304 With the exception of ``argv`` and ``arguments``, other args and kwargs
308 passed onto the constructor of :class:`argparse.ArgumentParser`.
305 arguments here are passed onto the constructor of
306 :class:`argparse.ArgumentParser`.
309
307
310 Parameters
308 Parameters
311 ----------
309 ----------
312
310
313 argv : optional, list
311 argv : optional, list
314 If given, used to read command-line arguments from, otherwise
312 If given, used to read command-line arguments from, otherwise
315 sys.argv[1:] is used.
313 sys.argv[1:] is used.
314
315 arguments : optional, tuple
316 Description of valid command-line arguments, to be called in sequence
317 with parser.add_argument() to configure the parser.
316 """
318 """
317 super(CommandLineConfigLoader, self).__init__()
319 super(CommandLineConfigLoader, self).__init__()
318 if argv == None:
320 if argv == None:
319 argv = sys.argv[1:]
321 argv = sys.argv[1:]
320 self.argv = argv
322 self.argv = argv
323 self.arguments = arguments
321 self.args = args
324 self.args = args
322 self.kw = kw
325 self.kw = kw
323
326
324 def load_config(self, args=None):
327 def load_config(self, args=None):
325 """Parse command line arguments and return as a Struct.
328 """Parse command line arguments and return as a Struct.
326
329
327 Parameters
330 Parameters
328 ----------
331 ----------
329
332
330 args : optional, list
333 args : optional, list
331 If given, a list with the structure of sys.argv[1:] to parse arguments
334 If given, a list with the structure of sys.argv[1:] to parse arguments
332 from. If not given, the instance's self.argv attribute (given at
335 from. If not given, the instance's self.argv attribute (given at
333 construction time) is used."""
336 construction time) is used."""
334
337
335 if args is None:
338 if args is None:
336 args = self.argv
339 args = self.argv
337 self._create_parser()
340 self._create_parser()
338 self._parse_args(args)
341 self._parse_args(args)
339 self._convert_to_config()
342 self._convert_to_config()
340 return self.config
343 return self.config
341
344
342 def get_extra_args(self):
345 def get_extra_args(self):
343 if hasattr(self, 'extra_args'):
346 if hasattr(self, 'extra_args'):
344 return self.extra_args
347 return self.extra_args
345 else:
348 else:
346 return []
349 return []
347
350
348 def _create_parser(self):
351 def _create_parser(self):
349 self.parser = ArgumentParser(*self.args, **self.kw)
352 self.parser = ArgumentParser(*self.args, **self.kw)
350 self._add_arguments()
353 self._add_arguments()
351 self._add_other_arguments()
354 self._add_other_arguments()
352
355
353 def _add_other_arguments(self):
356 def _add_other_arguments(self):
354 pass
357 pass
355
358
356 def _add_arguments(self):
359 def _add_arguments(self):
357 for argument in self.arguments:
360 for argument in self.arguments:
358 argument[1].setdefault('default', NoConfigDefault)
361 argument[1].setdefault('default', NoConfigDefault)
359 self.parser.add_argument(*argument[0],**argument[1])
362 self.parser.add_argument(*argument[0],**argument[1])
360
363
361 def _parse_args(self, args):
364 def _parse_args(self, args):
362 """self.parser->self.parsed_data"""
365 """self.parser->self.parsed_data"""
363 self.parsed_data, self.extra_args = self.parser.parse_known_args(args)
366 self.parsed_data, self.extra_args = self.parser.parse_known_args(args)
364
367
365 def _convert_to_config(self):
368 def _convert_to_config(self):
366 """self.parsed_data->self.config"""
369 """self.parsed_data->self.config"""
367 for k, v in vars(self.parsed_data).items():
370 for k, v in vars(self.parsed_data).items():
368 if v is not NoConfigDefault:
371 if v is not NoConfigDefault:
369 exec_str = 'self.config.' + k + '= v'
372 exec_str = 'self.config.' + k + '= v'
370 exec exec_str in locals(), globals()
373 exec exec_str in locals(), globals()
@@ -1,164 +1,162 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 Tests for IPython.config.loader
4 Tests for IPython.config.loader
5
5
6 Authors:
6 Authors:
7
7
8 * Brian Granger
8 * Brian Granger
9 * Fernando Perez (design help)
9 * Fernando Perez (design help)
10 """
10 """
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Copyright (C) 2008-2009 The IPython Development Team
13 # Copyright (C) 2008-2009 The IPython Development Team
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
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Imports
20 # Imports
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 import os
23 import os
24 from tempfile import mkstemp
24 from tempfile import mkstemp
25 from unittest import TestCase
25 from unittest import TestCase
26
26
27 from IPython.config.loader import (
27 from IPython.config.loader import (
28 Config,
28 Config,
29 PyFileConfigLoader,
29 PyFileConfigLoader,
30 ArgParseConfigLoader,
30 ArgParseConfigLoader,
31 ConfigError
31 ConfigError
32 )
32 )
33
33
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35 # Actual tests
35 # Actual tests
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37
37
38
38
39 pyfile = """
39 pyfile = """
40 c = get_config()
40 c = get_config()
41 c.a = 10
41 c.a = 10
42 c.b = 20
42 c.b = 20
43 c.Foo.Bar.value = 10
43 c.Foo.Bar.value = 10
44 c.Foo.Bam.value = range(10)
44 c.Foo.Bam.value = range(10)
45 c.D.C.value = 'hi there'
45 c.D.C.value = 'hi there'
46 """
46 """
47
47
48 class TestPyFileCL(TestCase):
48 class TestPyFileCL(TestCase):
49
49
50 def test_basic(self):
50 def test_basic(self):
51 fd, fname = mkstemp('.py')
51 fd, fname = mkstemp('.py')
52 f = os.fdopen(fd, 'w')
52 f = os.fdopen(fd, 'w')
53 f.write(pyfile)
53 f.write(pyfile)
54 f.close()
54 f.close()
55 # Unlink the file
55 # Unlink the file
56 cl = PyFileConfigLoader(fname)
56 cl = PyFileConfigLoader(fname)
57 config = cl.load_config()
57 config = cl.load_config()
58 self.assertEquals(config.a, 10)
58 self.assertEquals(config.a, 10)
59 self.assertEquals(config.b, 20)
59 self.assertEquals(config.b, 20)
60 self.assertEquals(config.Foo.Bar.value, 10)
60 self.assertEquals(config.Foo.Bar.value, 10)
61 self.assertEquals(config.Foo.Bam.value, range(10))
61 self.assertEquals(config.Foo.Bam.value, range(10))
62 self.assertEquals(config.D.C.value, 'hi there')
62 self.assertEquals(config.D.C.value, 'hi there')
63
63
64
64
65 class TestArgParseCL(TestCase):
65 class TestArgParseCL(TestCase):
66
66
67 def test_basic(self):
67 def test_basic(self):
68
68
69 class MyLoader(ArgParseConfigLoader):
69 arguments = (
70 arguments = (
71 (('-f','--foo'), dict(dest='Global.foo', type=str)),
70 (('-f','--foo'), dict(dest='Global.foo', type=str)),
72 (('-b',), dict(dest='MyClass.bar', type=int)),
71 (('-b',), dict(dest='MyClass.bar', type=int)),
73 (('-n',), dict(dest='n', action='store_true')),
72 (('-n',), dict(dest='n', action='store_true')),
74 (('Global.bam',), dict(type=str))
73 (('Global.bam',), dict(type=str))
75 )
74 )
76
75 cl = ArgParseConfigLoader(arguments=arguments)
77 cl = MyLoader()
78 config = cl.load_config('-f hi -b 10 -n wow'.split())
76 config = cl.load_config('-f hi -b 10 -n wow'.split())
79 self.assertEquals(config.Global.foo, 'hi')
77 self.assertEquals(config.Global.foo, 'hi')
80 self.assertEquals(config.MyClass.bar, 10)
78 self.assertEquals(config.MyClass.bar, 10)
81 self.assertEquals(config.n, True)
79 self.assertEquals(config.n, True)
82 self.assertEquals(config.Global.bam, 'wow')
80 self.assertEquals(config.Global.bam, 'wow')
83
81
84 def test_add_arguments(self):
82 def test_add_arguments(self):
85
83
86 class MyLoader(ArgParseConfigLoader):
84 class MyLoader(ArgParseConfigLoader):
87 def _add_arguments(self):
85 def _add_arguments(self):
88 subparsers = self.parser.add_subparsers(dest='subparser_name')
86 subparsers = self.parser.add_subparsers(dest='subparser_name')
89 subparser1 = subparsers.add_parser('1')
87 subparser1 = subparsers.add_parser('1')
90 subparser1.add_argument('-x',dest='Global.x')
88 subparser1.add_argument('-x',dest='Global.x')
91 subparser2 = subparsers.add_parser('2')
89 subparser2 = subparsers.add_parser('2')
92 subparser2.add_argument('y')
90 subparser2.add_argument('y')
93
91
94 cl = MyLoader()
92 cl = MyLoader()
95 config = cl.load_config('2 frobble'.split())
93 config = cl.load_config('2 frobble'.split())
96 self.assertEquals(config.subparser_name, '2')
94 self.assertEquals(config.subparser_name, '2')
97 self.assertEquals(config.y, 'frobble')
95 self.assertEquals(config.y, 'frobble')
98 config = cl.load_config('1 -x frobble'.split())
96 config = cl.load_config('1 -x frobble'.split())
99 self.assertEquals(config.subparser_name, '1')
97 self.assertEquals(config.subparser_name, '1')
100 self.assertEquals(config.Global.x, 'frobble')
98 self.assertEquals(config.Global.x, 'frobble')
101
99
102 class TestConfig(TestCase):
100 class TestConfig(TestCase):
103
101
104 def test_setget(self):
102 def test_setget(self):
105 c = Config()
103 c = Config()
106 c.a = 10
104 c.a = 10
107 self.assertEquals(c.a, 10)
105 self.assertEquals(c.a, 10)
108 self.assertEquals(c.has_key('b'), False)
106 self.assertEquals(c.has_key('b'), False)
109
107
110 def test_auto_section(self):
108 def test_auto_section(self):
111 c = Config()
109 c = Config()
112 self.assertEquals(c.has_key('A'), True)
110 self.assertEquals(c.has_key('A'), True)
113 self.assertEquals(c._has_section('A'), False)
111 self.assertEquals(c._has_section('A'), False)
114 A = c.A
112 A = c.A
115 A.foo = 'hi there'
113 A.foo = 'hi there'
116 self.assertEquals(c._has_section('A'), True)
114 self.assertEquals(c._has_section('A'), True)
117 self.assertEquals(c.A.foo, 'hi there')
115 self.assertEquals(c.A.foo, 'hi there')
118 del c.A
116 del c.A
119 self.assertEquals(len(c.A.keys()),0)
117 self.assertEquals(len(c.A.keys()),0)
120
118
121 def test_merge_doesnt_exist(self):
119 def test_merge_doesnt_exist(self):
122 c1 = Config()
120 c1 = Config()
123 c2 = Config()
121 c2 = Config()
124 c2.bar = 10
122 c2.bar = 10
125 c2.Foo.bar = 10
123 c2.Foo.bar = 10
126 c1._merge(c2)
124 c1._merge(c2)
127 self.assertEquals(c1.Foo.bar, 10)
125 self.assertEquals(c1.Foo.bar, 10)
128 self.assertEquals(c1.bar, 10)
126 self.assertEquals(c1.bar, 10)
129 c2.Bar.bar = 10
127 c2.Bar.bar = 10
130 c1._merge(c2)
128 c1._merge(c2)
131 self.assertEquals(c1.Bar.bar, 10)
129 self.assertEquals(c1.Bar.bar, 10)
132
130
133 def test_merge_exists(self):
131 def test_merge_exists(self):
134 c1 = Config()
132 c1 = Config()
135 c2 = Config()
133 c2 = Config()
136 c1.Foo.bar = 10
134 c1.Foo.bar = 10
137 c1.Foo.bam = 30
135 c1.Foo.bam = 30
138 c2.Foo.bar = 20
136 c2.Foo.bar = 20
139 c2.Foo.wow = 40
137 c2.Foo.wow = 40
140 c1._merge(c2)
138 c1._merge(c2)
141 self.assertEquals(c1.Foo.bam, 30)
139 self.assertEquals(c1.Foo.bam, 30)
142 self.assertEquals(c1.Foo.bar, 20)
140 self.assertEquals(c1.Foo.bar, 20)
143 self.assertEquals(c1.Foo.wow, 40)
141 self.assertEquals(c1.Foo.wow, 40)
144 c2.Foo.Bam.bam = 10
142 c2.Foo.Bam.bam = 10
145 c1._merge(c2)
143 c1._merge(c2)
146 self.assertEquals(c1.Foo.Bam.bam, 10)
144 self.assertEquals(c1.Foo.Bam.bam, 10)
147
145
148 def test_deepcopy(self):
146 def test_deepcopy(self):
149 c1 = Config()
147 c1 = Config()
150 c1.Foo.bar = 10
148 c1.Foo.bar = 10
151 c1.Foo.bam = 30
149 c1.Foo.bam = 30
152 c1.a = 'asdf'
150 c1.a = 'asdf'
153 c1.b = range(10)
151 c1.b = range(10)
154 import copy
152 import copy
155 c2 = copy.deepcopy(c1)
153 c2 = copy.deepcopy(c1)
156 self.assertEquals(c1, c2)
154 self.assertEquals(c1, c2)
157 self.assert_(c1 is not c2)
155 self.assert_(c1 is not c2)
158 self.assert_(c1.Foo is not c2.Foo)
156 self.assert_(c1.Foo is not c2.Foo)
159
157
160 def test_builtin(self):
158 def test_builtin(self):
161 c1 = Config()
159 c1 = Config()
162 exec 'foo = True' in c1
160 exec 'foo = True' in c1
163 self.assertEquals(c1.foo, True)
161 self.assertEquals(c1.foo, True)
164 self.assertRaises(ConfigError, setattr, c1, 'ValueError', 10)
162 self.assertRaises(ConfigError, setattr, c1, 'ValueError', 10)
@@ -1,390 +1,431 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 An application for IPython.
4 An application for IPython.
5
5
6 All top-level applications should use the classes in this module for
6 All top-level applications should use the classes in this module for
7 handling configuration and creating componenets.
7 handling configuration and creating componenets.
8
8
9 The job of an :class:`Application` is to create the master configuration
9 The job of an :class:`Application` is to create the master configuration
10 object and then create the components, passing the config to them.
10 object and then create the components, passing the config to them.
11
11
12 Authors:
12 Authors:
13
13
14 * Brian Granger
14 * Brian Granger
15 * Fernando Perez
15 * Fernando Perez
16
16
17 Notes
17 Notes
18 -----
18 -----
19 """
19 """
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Copyright (C) 2008-2009 The IPython Development Team
22 # Copyright (C) 2008-2009 The IPython Development Team
23 #
23 #
24 # Distributed under the terms of the BSD License. The full license is in
24 # Distributed under the terms of the BSD License. The full license is in
25 # the file COPYING, distributed as part of this software.
25 # the file COPYING, distributed as part of this software.
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27
27
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 # Imports
29 # Imports
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31
31
32 import logging
32 import logging
33 import os
33 import os
34 import sys
34 import sys
35
35
36 from IPython.core import release
36 from IPython.core import release, crashhandler
37 from IPython.utils.genutils import get_ipython_dir, get_ipython_package_dir
37 from IPython.utils.genutils import get_ipython_dir, get_ipython_package_dir
38 from IPython.config.loader import (
38 from IPython.config.loader import (
39 PyFileConfigLoader,
39 PyFileConfigLoader,
40 ArgParseConfigLoader,
40 ArgParseConfigLoader,
41 Config,
41 Config,
42 NoConfigDefault
42 NoConfigDefault
43 )
43 )
44
44
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46 # Classes and functions
46 # Classes and functions
47 #-----------------------------------------------------------------------------
47 #-----------------------------------------------------------------------------
48
48
49
49
50 class BaseAppArgParseConfigLoader(ArgParseConfigLoader):
50 class BaseAppArgParseConfigLoader(ArgParseConfigLoader):
51 """Default command line options for IPython based applications."""
51 """Default command line options for IPython based applications."""
52
52
53 def _add_other_arguments(self):
53 def _add_other_arguments(self):
54 self.parser.add_argument('--ipython-dir',
54 self.parser.add_argument('--ipython-dir',
55 dest='Global.ipython_dir',type=unicode,
55 dest='Global.ipython_dir',type=unicode,
56 help='Set to override default location of Global.ipython_dir.',
56 help='Set to override default location of Global.ipython_dir.',
57 default=NoConfigDefault,
57 default=NoConfigDefault,
58 metavar='Global.ipython_dir')
58 metavar='Global.ipython_dir')
59 self.parser.add_argument('-p', '--profile',
59 self.parser.add_argument('-p', '--profile',
60 dest='Global.profile',type=unicode,
60 dest='Global.profile',type=unicode,
61 help='The string name of the ipython profile to be used.',
61 help='The string name of the ipython profile to be used.',
62 default=NoConfigDefault,
62 default=NoConfigDefault,
63 metavar='Global.profile')
63 metavar='Global.profile')
64 self.parser.add_argument('--log-level',
64 self.parser.add_argument('--log-level',
65 dest="Global.log_level",type=int,
65 dest="Global.log_level",type=int,
66 help='Set the log level (0,10,20,30,40,50). Default is 30.',
66 help='Set the log level (0,10,20,30,40,50). Default is 30.',
67 default=NoConfigDefault,
67 default=NoConfigDefault,
68 metavar='Global.log_level')
68 metavar='Global.log_level')
69 self.parser.add_argument('--config-file',
69 self.parser.add_argument('--config-file',
70 dest='Global.config_file',type=unicode,
70 dest='Global.config_file',type=unicode,
71 help='Set the config file name to override default.',
71 help='Set the config file name to override default.',
72 default=NoConfigDefault,
72 default=NoConfigDefault,
73 metavar='Global.config_file')
73 metavar='Global.config_file')
74
74
75
75
76 class ApplicationError(Exception):
76 class ApplicationError(Exception):
77 pass
77 pass
78
78
79
79
80 app_cl_args = (
81 (('--ipython-dir', ), dict(
82 dest='Global.ipython_dir',type=unicode,
83 help='Set to override default location of Global.ipython_dir.',
84 default=NoConfigDefault,
85 metavar='Global.ipython_dir') ),
86 (('-p', '--profile',), dict(
87 dest='Global.profile',type=unicode,
88 help='The string name of the ipython profile to be used.',
89 default=NoConfigDefault,
90 metavar='Global.profile') ),
91 (('--log-level',), dict(
92 dest="Global.log_level",type=int,
93 help='Set the log level (0,10,20,30,40,50). Default is 30.',
94 default=NoConfigDefault,
95 metavar='Global.log_level')),
96 (('--config-file',), dict(
97 dest='Global.config_file',type=unicode,
98 help='Set the config file name to override default.',
99 default=NoConfigDefault,
100 metavar='Global.config_file')),
101 )
102
80 class Application(object):
103 class Application(object):
81 """Load a config, construct components and set them running."""
104 """Load a config, construct components and set them running."""
82
105
83 name = u'ipython'
106 name = u'ipython'
84 description = 'IPython: an enhanced interactive Python shell.'
107 description = 'IPython: an enhanced interactive Python shell.'
85
108
86 config_file_name = u'ipython_config.py'
109 config_file_name = u'ipython_config.py'
87 # Track the default and actual separately because some messages are
110 # Track the default and actual separately because some messages are
88 # only printed if we aren't using the default.
111 # only printed if we aren't using the default.
89 default_config_file_name = config_file_name
112 default_config_file_name = config_file_name
90 default_log_level = logging.WARN
113 default_log_level = logging.WARN
91 # Set by --profile option
114 # Set by --profile option
92 profile_name = None
115 profile_name = None
93 #: User's ipython directory, typically ~/.ipython/
116 #: User's ipython directory, typically ~/.ipython/
94 ipython_dir = None
117 ipython_dir = None
95 #: A reference to the argv to be used (typically ends up being sys.argv[1:])
118 #: A reference to the argv to be used (typically ends up being sys.argv[1:])
96 argv = None
119 argv = None
120 #: Default command line arguments. Subclasses should create a new tuple
121 #: that *includes* these.
122 cl_arguments = app_cl_args
97
123
98 # Private attributes
124 # Private attributes
99 _exiting = False
125 _exiting = False
100 _initialized = False
126 _initialized = False
101
127
128 # Class choices for things that will be instantiated at runtime.
129 _CrashHandler = crashhandler.CrashHandler
130
102 def __init__(self, argv=None):
131 def __init__(self, argv=None):
103 self.argv = sys.argv[1:] if argv is None else argv
132 self.argv = sys.argv[1:] if argv is None else argv
104 self.init_logger()
133 self.init_logger()
105
134
106 def init_logger(self):
135 def init_logger(self):
107 self.log = logging.getLogger(self.__class__.__name__)
136 self.log = logging.getLogger(self.__class__.__name__)
108 # This is used as the default until the command line arguments are read.
137 # This is used as the default until the command line arguments are read.
109 self.log.setLevel(self.default_log_level)
138 self.log.setLevel(self.default_log_level)
110 self._log_handler = logging.StreamHandler()
139 self._log_handler = logging.StreamHandler()
111 self._log_formatter = logging.Formatter("[%(name)s] %(message)s")
140 self._log_formatter = logging.Formatter("[%(name)s] %(message)s")
112 self._log_handler.setFormatter(self._log_formatter)
141 self._log_handler.setFormatter(self._log_formatter)
113 self.log.addHandler(self._log_handler)
142 self.log.addHandler(self._log_handler)
114
143
115 def _set_log_level(self, level):
144 def _set_log_level(self, level):
116 self.log.setLevel(level)
145 self.log.setLevel(level)
117
146
118 def _get_log_level(self):
147 def _get_log_level(self):
119 return self.log.level
148 return self.log.level
120
149
121 log_level = property(_get_log_level, _set_log_level)
150 log_level = property(_get_log_level, _set_log_level)
122
151
123 def initialize(self):
152 def initialize(self):
124 """Start the application."""
153 """Start the application."""
125
154
126 if self._initialized:
155 if self._initialized:
127 return
156 return
128
157
129 self.attempt(self.create_default_config)
158 # The first part is protected with an 'attempt' wrapper, that will log
159 # failures with the basic system traceback machinery. Once our crash
160 # handler is in place, we can let any subsequent exception propagate,
161 # as our handler will log it with much better detail than the default.
162 self.attempt(self.create_crash_handler)
163 self.create_default_config()
130 self.log_default_config()
164 self.log_default_config()
131 self.set_default_config_log_level()
165 self.set_default_config_log_level()
132 self.attempt(self.pre_load_command_line_config)
166 self.pre_load_command_line_config()
133 self.attempt(self.load_command_line_config, action='abort')
167 self.load_command_line_config()
134 self.set_command_line_config_log_level()
168 self.set_command_line_config_log_level()
135 self.attempt(self.post_load_command_line_config)
169 self.post_load_command_line_config()
136 self.log_command_line_config()
170 self.log_command_line_config()
137 self.attempt(self.find_ipython_dir)
171 self.find_ipython_dir()
138 self.attempt(self.find_resources)
172 self.find_resources()
139 self.attempt(self.find_config_file_name)
173 self.find_config_file_name()
140 self.attempt(self.find_config_file_paths)
174 self.find_config_file_paths()
141 self.attempt(self.pre_load_file_config)
175 self.pre_load_file_config()
142 self.attempt(self.load_file_config)
176 self.load_file_config()
143 self.set_file_config_log_level()
177 self.set_file_config_log_level()
144 self.attempt(self.post_load_file_config)
178 self.post_load_file_config()
145 self.log_file_config()
179 self.log_file_config()
146 self.attempt(self.merge_configs)
180 self.merge_configs()
147 self.log_master_config()
181 self.log_master_config()
148 self.attempt(self.pre_construct)
182 self.pre_construct()
149 self.attempt(self.construct)
183 self.construct()
150 self.attempt(self.post_construct)
184 self.post_construct()
151 self._initialized = True
185 self._initialized = True
152
186
153 def start(self):
187 def start(self):
154 self.initialize()
188 self.initialize()
155 self.attempt(self.start_app)
189 self.start_app()
156
190
157 #-------------------------------------------------------------------------
191 #-------------------------------------------------------------------------
158 # Various stages of Application creation
192 # Various stages of Application creation
159 #-------------------------------------------------------------------------
193 #-------------------------------------------------------------------------
160
194
195 def create_crash_handler(self):
196 """Create a crash handler, typically setting sys.excepthook to it."""
197 self.crash_handler = self._CrashHandler(self, self.name)
198 sys.excepthook = self.crash_handler
199
161 def create_default_config(self):
200 def create_default_config(self):
162 """Create defaults that can't be set elsewhere.
201 """Create defaults that can't be set elsewhere.
163
202
164 For the most part, we try to set default in the class attributes
203 For the most part, we try to set default in the class attributes
165 of Components. But, defaults the top-level Application (which is
204 of Components. But, defaults the top-level Application (which is
166 not a HasTraitlets or Component) are not set in this way. Instead
205 not a HasTraitlets or Component) are not set in this way. Instead
167 we set them here. The Global section is for variables like this that
206 we set them here. The Global section is for variables like this that
168 don't belong to a particular component.
207 don't belong to a particular component.
169 """
208 """
170 c = Config()
209 c = Config()
171 c.Global.ipython_dir = get_ipython_dir()
210 c.Global.ipython_dir = get_ipython_dir()
172 c.Global.log_level = self.log_level
211 c.Global.log_level = self.log_level
173 self.default_config = c
212 self.default_config = c
174
213
175 def log_default_config(self):
214 def log_default_config(self):
176 self.log.debug('Default config loaded:')
215 self.log.debug('Default config loaded:')
177 self.log.debug(repr(self.default_config))
216 self.log.debug(repr(self.default_config))
178
217
179 def set_default_config_log_level(self):
218 def set_default_config_log_level(self):
180 try:
219 try:
181 self.log_level = self.default_config.Global.log_level
220 self.log_level = self.default_config.Global.log_level
182 except AttributeError:
221 except AttributeError:
183 # Fallback to the default_log_level class attribute
222 # Fallback to the default_log_level class attribute
184 pass
223 pass
185
224
186 def create_command_line_config(self):
225 def create_command_line_config(self):
187 """Create and return a command line config loader."""
226 """Create and return a command line config loader."""
188 return BaseAppArgParseConfigLoader(self.argv,
227 return ArgParseConfigLoader(self.argv, self.cl_arguments,
189 description=self.description,
228 description=self.description,
190 version=release.version
229 version=release.version)
191 )
192
230
193 def pre_load_command_line_config(self):
231 def pre_load_command_line_config(self):
194 """Do actions just before loading the command line config."""
232 """Do actions just before loading the command line config."""
195 pass
233 pass
196
234
197 def load_command_line_config(self):
235 def load_command_line_config(self):
198 """Load the command line config."""
236 """Load the command line config."""
199 loader = self.create_command_line_config()
237 loader = self.create_command_line_config()
200 self.command_line_config = loader.load_config()
238 self.command_line_config = loader.load_config()
201 self.extra_args = loader.get_extra_args()
239 self.extra_args = loader.get_extra_args()
202
240
203 def set_command_line_config_log_level(self):
241 def set_command_line_config_log_level(self):
204 try:
242 try:
205 self.log_level = self.command_line_config.Global.log_level
243 self.log_level = self.command_line_config.Global.log_level
206 except AttributeError:
244 except AttributeError:
207 pass
245 pass
208
246
209 def post_load_command_line_config(self):
247 def post_load_command_line_config(self):
210 """Do actions just after loading the command line config."""
248 """Do actions just after loading the command line config."""
211 pass
249 pass
212
250
213 def log_command_line_config(self):
251 def log_command_line_config(self):
214 self.log.debug("Command line config loaded:")
252 self.log.debug("Command line config loaded:")
215 self.log.debug(repr(self.command_line_config))
253 self.log.debug(repr(self.command_line_config))
216
254
217 def find_ipython_dir(self):
255 def find_ipython_dir(self):
218 """Set the IPython directory.
256 """Set the IPython directory.
219
257
220 This sets ``self.ipython_dir``, but the actual value that is passed to
258 This sets ``self.ipython_dir``, but the actual value that is passed to
221 the application is kept in either ``self.default_config`` or
259 the application is kept in either ``self.default_config`` or
222 ``self.command_line_config``. This also adds ``self.ipython_dir`` to
260 ``self.command_line_config``. This also adds ``self.ipython_dir`` to
223 ``sys.path`` so config files there can be referenced by other config
261 ``sys.path`` so config files there can be referenced by other config
224 files.
262 files.
225 """
263 """
226
264
227 try:
265 try:
228 self.ipython_dir = self.command_line_config.Global.ipython_dir
266 self.ipython_dir = self.command_line_config.Global.ipython_dir
229 except AttributeError:
267 except AttributeError:
230 self.ipython_dir = self.default_config.Global.ipython_dir
268 self.ipython_dir = self.default_config.Global.ipython_dir
231 sys.path.append(os.path.abspath(self.ipython_dir))
269 sys.path.append(os.path.abspath(self.ipython_dir))
232 if not os.path.isdir(self.ipython_dir):
270 if not os.path.isdir(self.ipython_dir):
233 os.makedirs(self.ipython_dir, mode=0777)
271 os.makedirs(self.ipython_dir, mode=0777)
234 self.log.debug("IPYTHON_DIR set to: %s" % self.ipython_dir)
272 self.log.debug("IPYTHON_DIR set to: %s" % self.ipython_dir)
235
273
236 def find_resources(self):
274 def find_resources(self):
237 """Find other resources that need to be in place.
275 """Find other resources that need to be in place.
238
276
239 Things like cluster directories need to be in place to find the
277 Things like cluster directories need to be in place to find the
240 config file. These happen right after the IPython directory has
278 config file. These happen right after the IPython directory has
241 been set.
279 been set.
242 """
280 """
243 pass
281 pass
244
282
245 def find_config_file_name(self):
283 def find_config_file_name(self):
246 """Find the config file name for this application.
284 """Find the config file name for this application.
247
285
248 This must set ``self.config_file_name`` to the filename of the
286 This must set ``self.config_file_name`` to the filename of the
249 config file to use (just the filename). The search paths for the
287 config file to use (just the filename). The search paths for the
250 config file are set in :meth:`find_config_file_paths` and then passed
288 config file are set in :meth:`find_config_file_paths` and then passed
251 to the config file loader where they are resolved to an absolute path.
289 to the config file loader where they are resolved to an absolute path.
252
290
253 If a profile has been set at the command line, this will resolve it.
291 If a profile has been set at the command line, this will resolve it.
254 """
292 """
255
293
256 try:
294 try:
257 self.config_file_name = self.command_line_config.Global.config_file
295 self.config_file_name = self.command_line_config.Global.config_file
258 except AttributeError:
296 except AttributeError:
259 pass
297 pass
260
298
261 try:
299 try:
262 self.profile_name = self.command_line_config.Global.profile
300 self.profile_name = self.command_line_config.Global.profile
263 except AttributeError:
301 except AttributeError:
264 pass
302 pass
265 else:
303 else:
266 name_parts = self.config_file_name.split('.')
304 name_parts = self.config_file_name.split('.')
267 name_parts.insert(1, u'_' + self.profile_name + u'.')
305 name_parts.insert(1, u'_' + self.profile_name + u'.')
268 self.config_file_name = ''.join(name_parts)
306 self.config_file_name = ''.join(name_parts)
269
307
270 def find_config_file_paths(self):
308 def find_config_file_paths(self):
271 """Set the search paths for resolving the config file.
309 """Set the search paths for resolving the config file.
272
310
273 This must set ``self.config_file_paths`` to a sequence of search
311 This must set ``self.config_file_paths`` to a sequence of search
274 paths to pass to the config file loader.
312 paths to pass to the config file loader.
275 """
313 """
276 # Include our own profiles directory last, so that users can still find
314 # Include our own profiles directory last, so that users can still find
277 # our shipped copies of builtin profiles even if they don't have them
315 # our shipped copies of builtin profiles even if they don't have them
278 # in their local ipython directory.
316 # in their local ipython directory.
279 prof_dir = os.path.join(get_ipython_package_dir(), 'config', 'profile')
317 prof_dir = os.path.join(get_ipython_package_dir(), 'config', 'profile')
280 self.config_file_paths = (os.getcwd(), self.ipython_dir, prof_dir)
318 self.config_file_paths = (os.getcwd(), self.ipython_dir, prof_dir)
281
319
282 def pre_load_file_config(self):
320 def pre_load_file_config(self):
283 """Do actions before the config file is loaded."""
321 """Do actions before the config file is loaded."""
284 pass
322 pass
285
323
286 def load_file_config(self):
324 def load_file_config(self):
287 """Load the config file.
325 """Load the config file.
288
326
289 This tries to load the config file from disk. If successful, the
327 This tries to load the config file from disk. If successful, the
290 ``CONFIG_FILE`` config variable is set to the resolved config file
328 ``CONFIG_FILE`` config variable is set to the resolved config file
291 location. If not successful, an empty config is used.
329 location. If not successful, an empty config is used.
292 """
330 """
293 self.log.debug("Attempting to load config file: %s" %
331 self.log.debug("Attempting to load config file: %s" %
294 self.config_file_name)
332 self.config_file_name)
295 loader = PyFileConfigLoader(self.config_file_name,
333 loader = PyFileConfigLoader(self.config_file_name,
296 path=self.config_file_paths)
334 path=self.config_file_paths)
297 try:
335 try:
298 self.file_config = loader.load_config()
336 self.file_config = loader.load_config()
299 self.file_config.Global.config_file = loader.full_filename
337 self.file_config.Global.config_file = loader.full_filename
300 except IOError:
338 except IOError:
301 # Only warn if the default config file was NOT being used.
339 # Only warn if the default config file was NOT being used.
302 if not self.config_file_name==self.default_config_file_name:
340 if not self.config_file_name==self.default_config_file_name:
303 self.log.warn("Config file not found, skipping: %s" %
341 self.log.warn("Config file not found, skipping: %s" %
304 self.config_file_name, exc_info=True)
342 self.config_file_name, exc_info=True)
305 self.file_config = Config()
343 self.file_config = Config()
306 except:
344 except:
307 self.log.warn("Error loading config file: %s" %
345 self.log.warn("Error loading config file: %s" %
308 self.config_file_name, exc_info=True)
346 self.config_file_name, exc_info=True)
309 self.file_config = Config()
347 self.file_config = Config()
310
348
311 def set_file_config_log_level(self):
349 def set_file_config_log_level(self):
312 # We need to keeep self.log_level updated. But we only use the value
350 # We need to keeep self.log_level updated. But we only use the value
313 # of the file_config if a value was not specified at the command
351 # of the file_config if a value was not specified at the command
314 # line, because the command line overrides everything.
352 # line, because the command line overrides everything.
315 if not hasattr(self.command_line_config.Global, 'log_level'):
353 if not hasattr(self.command_line_config.Global, 'log_level'):
316 try:
354 try:
317 self.log_level = self.file_config.Global.log_level
355 self.log_level = self.file_config.Global.log_level
318 except AttributeError:
356 except AttributeError:
319 pass # Use existing value
357 pass # Use existing value
320
358
321 def post_load_file_config(self):
359 def post_load_file_config(self):
322 """Do actions after the config file is loaded."""
360 """Do actions after the config file is loaded."""
323 pass
361 pass
324
362
325 def log_file_config(self):
363 def log_file_config(self):
326 if hasattr(self.file_config.Global, 'config_file'):
364 if hasattr(self.file_config.Global, 'config_file'):
327 self.log.debug("Config file loaded: %s" %
365 self.log.debug("Config file loaded: %s" %
328 self.file_config.Global.config_file)
366 self.file_config.Global.config_file)
329 self.log.debug(repr(self.file_config))
367 self.log.debug(repr(self.file_config))
330
368
331 def merge_configs(self):
369 def merge_configs(self):
332 """Merge the default, command line and file config objects."""
370 """Merge the default, command line and file config objects."""
333 config = Config()
371 config = Config()
334 config._merge(self.default_config)
372 config._merge(self.default_config)
335 config._merge(self.file_config)
373 config._merge(self.file_config)
336 config._merge(self.command_line_config)
374 config._merge(self.command_line_config)
337 self.master_config = config
375 self.master_config = config
338
376
339 def log_master_config(self):
377 def log_master_config(self):
340 self.log.debug("Master config created:")
378 self.log.debug("Master config created:")
341 self.log.debug(repr(self.master_config))
379 self.log.debug(repr(self.master_config))
342
380
343 def pre_construct(self):
381 def pre_construct(self):
344 """Do actions after the config has been built, but before construct."""
382 """Do actions after the config has been built, but before construct."""
345 pass
383 pass
346
384
347 def construct(self):
385 def construct(self):
348 """Construct the main components that make up this app."""
386 """Construct the main components that make up this app."""
349 self.log.debug("Constructing components for application")
387 self.log.debug("Constructing components for application")
350
388
351 def post_construct(self):
389 def post_construct(self):
352 """Do actions after construct, but before starting the app."""
390 """Do actions after construct, but before starting the app."""
353 pass
391 pass
354
392
355 def start_app(self):
393 def start_app(self):
356 """Actually start the app."""
394 """Actually start the app."""
357 self.log.debug("Starting application")
395 self.log.debug("Starting application")
358
396
359 #-------------------------------------------------------------------------
397 #-------------------------------------------------------------------------
360 # Utility methods
398 # Utility methods
361 #-------------------------------------------------------------------------
399 #-------------------------------------------------------------------------
362
400
363 def abort(self):
401 def abort(self):
364 """Abort the starting of the application."""
402 """Abort the starting of the application."""
365 if self._exiting:
403 if self._exiting:
366 pass
404 pass
367 else:
405 else:
368 self.log.critical("Aborting application: %s" % self.name, exc_info=True)
406 self.log.critical("Aborting application: %s" % self.name, exc_info=True)
369 self._exiting = True
407 self._exiting = True
370 sys.exit(1)
408 sys.exit(1)
371
409
372 def exit(self, exit_status=0):
410 def exit(self, exit_status=0):
373 if self._exiting:
411 if self._exiting:
374 pass
412 pass
375 else:
413 else:
376 self.log.debug("Exiting application: %s" % self.name)
414 self.log.debug("Exiting application: %s" % self.name)
377 self._exiting = True
415 self._exiting = True
378 sys.exit(exit_status)
416 sys.exit(exit_status)
379
417
380 def attempt(self, func, action='abort'):
418 def attempt(self, func, action='abort'):
381 try:
419 try:
382 func()
420 func()
383 except SystemExit:
421 except SystemExit:
384 raise
422 raise
385 except:
423 except:
386 if action == 'abort':
424 if action == 'abort':
425 self.log.critical("Aborting application: %s" % self.name,
426 exc_info=True)
387 self.abort()
427 self.abort()
428 raise
388 elif action == 'exit':
429 elif action == 'exit':
389 self.exit(0)
430 self.exit(0)
390
431
@@ -1,229 +1,220 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """sys.excepthook for IPython itself, leaves a detailed report on disk.
2 """sys.excepthook for IPython itself, leaves a detailed report on disk.
3
3
4
4
5 Authors
5 Authors
6 -------
6 -------
7 - Fernando Perez <Fernando.Perez@berkeley.edu>
7 - Fernando Perez <Fernando.Perez@berkeley.edu>
8 """
8 """
9
9
10 #*****************************************************************************
10 #*****************************************************************************
11 # Copyright (C) 2008-2009 The IPython Development Team
11 # Copyright (C) 2008-2009 The IPython Development Team
12 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
12 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
13 #
13 #
14 # Distributed under the terms of the BSD License. The full license is in
14 # Distributed under the terms of the BSD License. The full license is in
15 # the file COPYING, distributed as part of this software.
15 # the file COPYING, distributed as part of this software.
16 #*****************************************************************************
16 #*****************************************************************************
17
17
18 #****************************************************************************
18 #****************************************************************************
19 # Required modules
19 # Required modules
20
20
21 # From the standard library
21 # From the standard library
22 import os
22 import os
23 import sys
23 import sys
24 from pprint import pformat
24 from pprint import pformat
25
25
26 # Our own
26 # Our own
27 from IPython.core import release
27 from IPython.core import release
28 from IPython.core import ultratb
28 from IPython.core import ultratb
29 from IPython.external.Itpl import itpl
29 from IPython.external.Itpl import itpl
30
30
31 from IPython.utils.genutils import *
32
33 #****************************************************************************
31 #****************************************************************************
34 class CrashHandler:
32 class CrashHandler(object):
35 """Customizable crash handlers for IPython-based systems.
33 """Customizable crash handlers for IPython-based systems.
36
34
37 Instances of this class provide a __call__ method which can be used as a
35 Instances of this class provide a __call__ method which can be used as a
38 sys.excepthook, i.e., the __call__ signature is:
36 sys.excepthook, i.e., the __call__ signature is:
39
37
40 def __call__(self,etype, evalue, etb)
38 def __call__(self,etype, evalue, etb)
41
39
42 """
40 """
43
41
44 def __init__(self,IP,app_name,contact_name,contact_email,
42 def __init__(self,app, app_name, contact_name=None, contact_email=None,
45 bug_tracker,crash_report_fname,
43 bug_tracker=None, crash_report_fname='CrashReport.txt',
46 show_crash_traceback=True):
44 show_crash_traceback=True):
47 """New crash handler.
45 """New crash handler.
48
46
49 Inputs:
47 Inputs:
50
48
51 - IP: a running IPython instance, which will be queried at crash time
49 - app: a running application instance, which will be queried at crash
52 for internal information.
50 time for internal information.
53
51
54 - app_name: a string containing the name of your application.
52 - app_name: a string containing the name of your application.
55
53
56 - contact_name: a string with the name of the person to contact.
54 - contact_name: a string with the name of the person to contact.
57
55
58 - contact_email: a string with the email address of the contact.
56 - contact_email: a string with the email address of the contact.
59
57
60 - bug_tracker: a string with the URL for your project's bug tracker.
58 - bug_tracker: a string with the URL for your project's bug tracker.
61
59
62 - crash_report_fname: a string with the filename for the crash report
60 - crash_report_fname: a string with the filename for the crash report
63 to be saved in. These reports are left in the ipython user directory
61 to be saved in. These reports are left in the ipython user directory
64 as determined by the running IPython instance.
62 as determined by the running IPython instance.
65
63
66 Optional inputs:
64 Optional inputs:
67
65
68 - show_crash_traceback(True): if false, don't print the crash
66 - show_crash_traceback(True): if false, don't print the crash
69 traceback on stderr, only generate the on-disk report
67 traceback on stderr, only generate the on-disk report
70
68
71
69
72 Non-argument instance attributes:
70 Non-argument instance attributes:
73
71
74 These instances contain some non-argument attributes which allow for
72 These instances contain some non-argument attributes which allow for
75 further customization of the crash handler's behavior. Please see the
73 further customization of the crash handler's behavior. Please see the
76 source for further details.
74 source for further details.
77 """
75 """
78
76
79 # apply args into instance
77 # apply args into instance
80 self.IP = IP # IPython instance
78 self.app = app
81 self.app_name = app_name
79 self.app_name = app_name
82 self.contact_name = contact_name
80 self.contact_name = contact_name
83 self.contact_email = contact_email
81 self.contact_email = contact_email
84 self.bug_tracker = bug_tracker
82 self.bug_tracker = bug_tracker
85 self.crash_report_fname = crash_report_fname
83 self.crash_report_fname = crash_report_fname
86 self.show_crash_traceback = show_crash_traceback
84 self.show_crash_traceback = show_crash_traceback
85 self.section_sep = '\n\n'+'*'*75+'\n\n'
87
86
88 # Hardcoded defaults, which can be overridden either by subclasses or
87 # Hardcoded defaults, which can be overridden either by subclasses or
89 # at runtime for the instance.
88 # at runtime for the instance.
90
89
91 # Template for the user message. Subclasses which completely override
90 # Template for the user message. Subclasses which completely override
92 # this, or user apps, can modify it to suit their tastes. It gets
91 # this, or user apps, can modify it to suit their tastes. It gets
93 # expanded using itpl, so calls of the kind $self.foo are valid.
92 # expanded using itpl, so calls of the kind $self.foo are valid.
94 self.user_message_template = """
93 self.user_message_template = """
95 Oops, $self.app_name crashed. We do our best to make it stable, but...
94 Oops, $self.app_name crashed. We do our best to make it stable, but...
96
95
97 A crash report was automatically generated with the following information:
96 A crash report was automatically generated with the following information:
98 - A verbatim copy of the crash traceback.
97 - A verbatim copy of the crash traceback.
99 - A copy of your input history during this session.
98 - A copy of your input history during this session.
100 - Data on your current $self.app_name configuration.
99 - Data on your current $self.app_name configuration.
101
100
102 It was left in the file named:
101 It was left in the file named:
103 \t'$self.crash_report_fname'
102 \t'$self.crash_report_fname'
104 If you can email this file to the developers, the information in it will help
103 If you can email this file to the developers, the information in it will help
105 them in understanding and correcting the problem.
104 them in understanding and correcting the problem.
106
105
107 You can mail it to: $self.contact_name at $self.contact_email
106 You can mail it to: $self.contact_name at $self.contact_email
108 with the subject '$self.app_name Crash Report'.
107 with the subject '$self.app_name Crash Report'.
109
108
110 If you want to do it now, the following command will work (under Unix):
109 If you want to do it now, the following command will work (under Unix):
111 mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname
110 mail -s '$self.app_name Crash Report' $self.contact_email < $self.crash_report_fname
112
111
113 To ensure accurate tracking of this issue, please file a report about it at:
112 To ensure accurate tracking of this issue, please file a report about it at:
114 $self.bug_tracker
113 $self.bug_tracker
115 """
114 """
116
115
117 def __call__(self,etype, evalue, etb):
116 def __call__(self,etype, evalue, etb):
118 """Handle an exception, call for compatible with sys.excepthook"""
117 """Handle an exception, call for compatible with sys.excepthook"""
119
118
120 # Report tracebacks shouldn't use color in general (safer for users)
119 # Report tracebacks shouldn't use color in general (safer for users)
121 color_scheme = 'NoColor'
120 color_scheme = 'NoColor'
122
121
123 # Use this ONLY for developer debugging (keep commented out for release)
122 # Use this ONLY for developer debugging (keep commented out for release)
124 #color_scheme = 'Linux' # dbg
123 #color_scheme = 'Linux' # dbg
125
124
126 try:
125 try:
127 rptdir = self.IP.ipython_dir
126 rptdir = self.app.ipython_dir
128 except:
127 except:
129 rptdir = os.getcwd()
128 rptdir = os.getcwd()
130 if not os.path.isdir(rptdir):
129 if not os.path.isdir(rptdir):
131 rptdir = os.getcwd()
130 rptdir = os.getcwd()
132 report_name = os.path.join(rptdir,self.crash_report_fname)
131 report_name = os.path.join(rptdir,self.crash_report_fname)
133 # write the report filename into the instance dict so it can get
132 # write the report filename into the instance dict so it can get
134 # properly expanded out in the user message template
133 # properly expanded out in the user message template
135 self.crash_report_fname = report_name
134 self.crash_report_fname = report_name
136 TBhandler = ultratb.VerboseTB(color_scheme=color_scheme,
135 TBhandler = ultratb.VerboseTB(color_scheme=color_scheme,
137 long_header=1)
136 long_header=1)
138 traceback = TBhandler.text(etype,evalue,etb,context=31)
137 traceback = TBhandler.text(etype,evalue,etb,context=31)
139
138
140 # print traceback to screen
139 # print traceback to screen
141 if self.show_crash_traceback:
140 if self.show_crash_traceback:
142 print >> sys.stderr, traceback
141 print >> sys.stderr, traceback
143
142
144 # and generate a complete report on disk
143 # and generate a complete report on disk
145 try:
144 try:
146 report = open(report_name,'w')
145 report = open(report_name,'w')
147 except:
146 except:
148 print >> sys.stderr, 'Could not create crash report on disk.'
147 print >> sys.stderr, 'Could not create crash report on disk.'
149 return
148 return
150
149
151 # Inform user on stderr of what happened
150 # Inform user on stderr of what happened
152 msg = itpl('\n'+'*'*70+'\n'+self.user_message_template)
151 msg = itpl('\n'+'*'*70+'\n'+self.user_message_template)
153 print >> sys.stderr, msg
152 print >> sys.stderr, msg
154
153
155 # Construct report on disk
154 # Construct report on disk
156 report.write(self.make_report(traceback))
155 report.write(self.make_report(traceback))
157 report.close()
156 report.close()
158 raw_input("Hit <Enter> to quit this message (your terminal may close):")
157 raw_input("Hit <Enter> to quit this message (your terminal may close):")
159
158
160 def make_report(self,traceback):
159 def make_report(self,traceback):
161 """Return a string containing a crash report."""
160 """Return a string containing a crash report."""
162
161 import platform
163 sec_sep = '\n\n'+'*'*75+'\n\n'
162
164
163 sec_sep = self.section_sep
164
165 report = []
165 report = []
166 rpt_add = report.append
166 rpt_add = report.append
167
167
168 rpt_add('*'*75+'\n\n'+'IPython post-mortem report\n\n')
168 rpt_add('*'*75+'\n\n'+'IPython post-mortem report\n\n')
169 rpt_add('IPython version: %s \n\n' % release.version)
169 rpt_add('IPython version: %s \n' % release.version)
170 rpt_add('BZR revision : %s \n\n' % release.revision)
170 rpt_add('BZR revision : %s \n' % release.revision)
171 rpt_add('Platform info : os.name -> %s, sys.platform -> %s' %
171 rpt_add('Platform info : os.name -> %s, sys.platform -> %s\n' %
172 (os.name,sys.platform) )
172 (os.name,sys.platform) )
173 rpt_add(sec_sep+'Current user configuration structure:\n\n')
173 rpt_add(' : %s\n' % platform.platform())
174 rpt_add(pformat(self.IP.dict()))
174 rpt_add('Python info : %s\n' % sys.version)
175 rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
175
176 try:
176 try:
177 rpt_add(sec_sep+"History of session input:")
177 config = pformat(self.app.config)
178 for line in self.IP.user_ns['_ih']:
178 rpt_add(sec_sep+'Current user configuration structure:\n\n')
179 rpt_add(line)
179 rpt_add(config)
180 rpt_add('\n*** Last line of input (may not be in above history):\n')
181 rpt_add(self.IP._last_input_line+'\n')
182 except:
180 except:
183 pass
181 pass
182 rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
184
183
185 return ''.join(report)
184 return ''.join(report)
186
185
186
187 class IPythonCrashHandler(CrashHandler):
187 class IPythonCrashHandler(CrashHandler):
188 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
188 """sys.excepthook for IPython itself, leaves a detailed report on disk."""
189
189
190 def __init__(self,IP):
190 def __init__(self, app, app_name='IPython'):
191
191
192 # Set here which of the IPython authors should be listed as contact
192 # Set here which of the IPython authors should be listed as contact
193 AUTHOR_CONTACT = 'Fernando'
193 AUTHOR_CONTACT = 'Fernando'
194
194
195 # Set argument defaults
195 # Set argument defaults
196 app_name = 'IPython'
197 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'
196 bug_tracker = 'https://bugs.launchpad.net/ipython/+filebug'
198 contact_name,contact_email = release.authors[AUTHOR_CONTACT][:2]
197 contact_name,contact_email = release.authors[AUTHOR_CONTACT][:2]
199 crash_report_fname = 'IPython_crash_report.txt'
198 crash_report_fname = 'IPython_crash_report.txt'
200 # Call parent constructor
199 # Call parent constructor
201 CrashHandler.__init__(self,IP,app_name,contact_name,contact_email,
200 CrashHandler.__init__(self,app,app_name,contact_name,contact_email,
202 bug_tracker,crash_report_fname)
201 bug_tracker,crash_report_fname)
203
202
204 def make_report(self,traceback):
203 def make_report(self,traceback):
205 """Return a string containing a crash report."""
204 """Return a string containing a crash report."""
206
205
207 sec_sep = '\n\n'+'*'*75+'\n\n'
206 sec_sep = self.section_sep
208
207 # Start with parent report
209 report = []
208 report = [super(IPythonCrashHandler, self).make_report(traceback)]
209 # Add interactive-specific info we may have
210 rpt_add = report.append
210 rpt_add = report.append
211
212 rpt_add('*'*75+'\n\n'+'IPython post-mortem report\n\n')
213 rpt_add('IPython version: %s \n\n' % release.version)
214 rpt_add('BZR revision : %s \n\n' % release.revision)
215 rpt_add('Platform info : os.name -> %s, sys.platform -> %s' %
216 (os.name,sys.platform) )
217 rpt_add(sec_sep+'Current user configuration structure:\n\n')
218 # rpt_add(pformat(self.IP.dict()))
219 rpt_add(sec_sep+'Crash traceback:\n\n' + traceback)
220 try:
211 try:
221 rpt_add(sec_sep+"History of session input:")
212 rpt_add(sec_sep+"History of session input:")
222 for line in self.IP.user_ns['_ih']:
213 for line in self.app.shell.user_ns['_ih']:
223 rpt_add(line)
214 rpt_add(line)
224 rpt_add('\n*** Last line of input (may not be in above history):\n')
215 rpt_add('\n*** Last line of input (may not be in above history):\n')
225 rpt_add(self.IP._last_input_line+'\n')
216 rpt_add(self.app.shell._last_input_line+'\n')
226 except:
217 except:
227 pass
218 pass
228
219
229 return ''.join(report)
220 return ''.join(report)
@@ -1,582 +1,576 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # encoding: utf-8
2 # encoding: utf-8
3 """
3 """
4 The :class:`~IPython.core.application.Application` object for the command
4 The :class:`~IPython.core.application.Application` object for the command
5 line :command:`ipython` program.
5 line :command:`ipython` program.
6
6
7 Authors:
7 Authors:
8
8
9 * Brian Granger
9 * Brian Granger
10 * Fernando Perez
10 * Fernando Perez
11
11
12 Notes
12 Notes
13 -----
13 -----
14 """
14 """
15
15
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Copyright (C) 2008-2009 The IPython Development Team
17 # Copyright (C) 2008-2009 The IPython Development Team
18 #
18 #
19 # Distributed under the terms of the BSD License. The full license is in
19 # Distributed under the terms of the BSD License. The full license is in
20 # the file COPYING, distributed as part of this software.
20 # the file COPYING, distributed as part of this software.
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24 # Imports
24 # Imports
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
26
27 import logging
27 import logging
28 import os
28 import os
29 import sys
29 import sys
30
30
31 from IPython.core import crashhandler
31 from IPython.core import release
32 from IPython.core import release
32 from IPython.core.application import Application, BaseAppArgParseConfigLoader
33 from IPython.core.application import Application, BaseAppArgParseConfigLoader
33 from IPython.core.error import UsageError
34 from IPython.core.error import UsageError
34 from IPython.core.iplib import InteractiveShell
35 from IPython.core.iplib import InteractiveShell
35 from IPython.core.pylabtools import pylab_activate
36 from IPython.core.pylabtools import pylab_activate
36 from IPython.config.loader import (
37 from IPython.config.loader import (
37 NoConfigDefault,
38 NoConfigDefault,
38 Config,
39 Config,
39 PyFileConfigLoader
40 PyFileConfigLoader
40 )
41 )
41 from IPython.lib import inputhook
42 from IPython.lib import inputhook
42 from IPython.utils.genutils import filefind, get_ipython_dir
43 from IPython.utils.genutils import filefind, get_ipython_dir
43
44
44 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
45 # Utilities and helpers
46 # Utilities and helpers
46 #-----------------------------------------------------------------------------
47 #-----------------------------------------------------------------------------
47
48
48 ipython_desc = """
49 ipython_desc = """
49 A Python shell with automatic history (input and output), dynamic object
50 A Python shell with automatic history (input and output), dynamic object
50 introspection, easier configuration, command completion, access to the system
51 introspection, easier configuration, command completion, access to the system
51 shell and more.
52 shell and more.
52 """
53 """
53
54
54 #-----------------------------------------------------------------------------
55 #-----------------------------------------------------------------------------
55 # Main classes and functions
56 # Main classes and functions
56 #-----------------------------------------------------------------------------
57 #-----------------------------------------------------------------------------
57
58
58 cl_args = (
59 cl_args = (
59 (('--autocall',), dict(
60 (('--autocall',), dict(
60 type=int, dest='InteractiveShell.autocall', default=NoConfigDefault,
61 type=int, dest='InteractiveShell.autocall', default=NoConfigDefault,
61 help='Set the autocall value (0,1,2).',
62 help='Set the autocall value (0,1,2).',
62 metavar='InteractiveShell.autocall')
63 metavar='InteractiveShell.autocall')
63 ),
64 ),
64 (('--autoindent',), dict(
65 (('--autoindent',), dict(
65 action='store_true', dest='InteractiveShell.autoindent', default=NoConfigDefault,
66 action='store_true', dest='InteractiveShell.autoindent', default=NoConfigDefault,
66 help='Turn on autoindenting.')
67 help='Turn on autoindenting.')
67 ),
68 ),
68 (('--no-autoindent',), dict(
69 (('--no-autoindent',), dict(
69 action='store_false', dest='InteractiveShell.autoindent', default=NoConfigDefault,
70 action='store_false', dest='InteractiveShell.autoindent', default=NoConfigDefault,
70 help='Turn off autoindenting.')
71 help='Turn off autoindenting.')
71 ),
72 ),
72 (('--automagic',), dict(
73 (('--automagic',), dict(
73 action='store_true', dest='InteractiveShell.automagic', default=NoConfigDefault,
74 action='store_true', dest='InteractiveShell.automagic', default=NoConfigDefault,
74 help='Turn on the auto calling of magic commands.')
75 help='Turn on the auto calling of magic commands.')
75 ),
76 ),
76 (('--no-automagic',), dict(
77 (('--no-automagic',), dict(
77 action='store_false', dest='InteractiveShell.automagic', default=NoConfigDefault,
78 action='store_false', dest='InteractiveShell.automagic', default=NoConfigDefault,
78 help='Turn off the auto calling of magic commands.')
79 help='Turn off the auto calling of magic commands.')
79 ),
80 ),
80 (('--autoedit-syntax',), dict(
81 (('--autoedit-syntax',), dict(
81 action='store_true', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,
82 action='store_true', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,
82 help='Turn on auto editing of files with syntax errors.')
83 help='Turn on auto editing of files with syntax errors.')
83 ),
84 ),
84 (('--no-autoedit-syntax',), dict(
85 (('--no-autoedit-syntax',), dict(
85 action='store_false', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,
86 action='store_false', dest='InteractiveShell.autoedit_syntax', default=NoConfigDefault,
86 help='Turn off auto editing of files with syntax errors.')
87 help='Turn off auto editing of files with syntax errors.')
87 ),
88 ),
88 (('--banner',), dict(
89 (('--banner',), dict(
89 action='store_true', dest='Global.display_banner', default=NoConfigDefault,
90 action='store_true', dest='Global.display_banner', default=NoConfigDefault,
90 help='Display a banner upon starting IPython.')
91 help='Display a banner upon starting IPython.')
91 ),
92 ),
92 (('--no-banner',), dict(
93 (('--no-banner',), dict(
93 action='store_false', dest='Global.display_banner', default=NoConfigDefault,
94 action='store_false', dest='Global.display_banner', default=NoConfigDefault,
94 help="Don't display a banner upon starting IPython.")
95 help="Don't display a banner upon starting IPython.")
95 ),
96 ),
96 (('--cache-size',), dict(
97 (('--cache-size',), dict(
97 type=int, dest='InteractiveShell.cache_size', default=NoConfigDefault,
98 type=int, dest='InteractiveShell.cache_size', default=NoConfigDefault,
98 help="Set the size of the output cache.",
99 help="Set the size of the output cache.",
99 metavar='InteractiveShell.cache_size')
100 metavar='InteractiveShell.cache_size')
100 ),
101 ),
101 (('--classic',), dict(
102 (('--classic',), dict(
102 action='store_true', dest='Global.classic', default=NoConfigDefault,
103 action='store_true', dest='Global.classic', default=NoConfigDefault,
103 help="Gives IPython a similar feel to the classic Python prompt.")
104 help="Gives IPython a similar feel to the classic Python prompt.")
104 ),
105 ),
105 (('--colors',), dict(
106 (('--colors',), dict(
106 type=str, dest='InteractiveShell.colors', default=NoConfigDefault,
107 type=str, dest='InteractiveShell.colors', default=NoConfigDefault,
107 help="Set the color scheme (NoColor, Linux, and LightBG).",
108 help="Set the color scheme (NoColor, Linux, and LightBG).",
108 metavar='InteractiveShell.colors')
109 metavar='InteractiveShell.colors')
109 ),
110 ),
110 (('--color-info',), dict(
111 (('--color-info',), dict(
111 action='store_true', dest='InteractiveShell.color_info', default=NoConfigDefault,
112 action='store_true', dest='InteractiveShell.color_info', default=NoConfigDefault,
112 help="Enable using colors for info related things.")
113 help="Enable using colors for info related things.")
113 ),
114 ),
114 (('--no-color-info',), dict(
115 (('--no-color-info',), dict(
115 action='store_false', dest='InteractiveShell.color_info', default=NoConfigDefault,
116 action='store_false', dest='InteractiveShell.color_info', default=NoConfigDefault,
116 help="Disable using colors for info related things.")
117 help="Disable using colors for info related things.")
117 ),
118 ),
118 (('--confirm-exit',), dict(
119 (('--confirm-exit',), dict(
119 action='store_true', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,
120 action='store_true', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,
120 help="Prompt the user when existing.")
121 help="Prompt the user when existing.")
121 ),
122 ),
122 (('--no-confirm-exit',), dict(
123 (('--no-confirm-exit',), dict(
123 action='store_false', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,
124 action='store_false', dest='InteractiveShell.confirm_exit', default=NoConfigDefault,
124 help="Don't prompt the user when existing.")
125 help="Don't prompt the user when existing.")
125 ),
126 ),
126 (('--deep-reload',), dict(
127 (('--deep-reload',), dict(
127 action='store_true', dest='InteractiveShell.deep_reload', default=NoConfigDefault,
128 action='store_true', dest='InteractiveShell.deep_reload', default=NoConfigDefault,
128 help="Enable deep (recursive) reloading by default.")
129 help="Enable deep (recursive) reloading by default.")
129 ),
130 ),
130 (('--no-deep-reload',), dict(
131 (('--no-deep-reload',), dict(
131 action='store_false', dest='InteractiveShell.deep_reload', default=NoConfigDefault,
132 action='store_false', dest='InteractiveShell.deep_reload', default=NoConfigDefault,
132 help="Disable deep (recursive) reloading by default.")
133 help="Disable deep (recursive) reloading by default.")
133 ),
134 ),
134 (('--editor',), dict(
135 (('--editor',), dict(
135 type=str, dest='InteractiveShell.editor', default=NoConfigDefault,
136 type=str, dest='InteractiveShell.editor', default=NoConfigDefault,
136 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
137 help="Set the editor used by IPython (default to $EDITOR/vi/notepad).",
137 metavar='InteractiveShell.editor')
138 metavar='InteractiveShell.editor')
138 ),
139 ),
139 (('--log','-l'), dict(
140 (('--log','-l'), dict(
140 action='store_true', dest='InteractiveShell.logstart', default=NoConfigDefault,
141 action='store_true', dest='InteractiveShell.logstart', default=NoConfigDefault,
141 help="Start logging to the default file (./ipython_log.py).")
142 help="Start logging to the default file (./ipython_log.py).")
142 ),
143 ),
143 (('--logfile','-lf'), dict(
144 (('--logfile','-lf'), dict(
144 type=unicode, dest='InteractiveShell.logfile', default=NoConfigDefault,
145 type=unicode, dest='InteractiveShell.logfile', default=NoConfigDefault,
145 help="Start logging to logfile.",
146 help="Start logging to logfile.",
146 metavar='InteractiveShell.logfile')
147 metavar='InteractiveShell.logfile')
147 ),
148 ),
148 (('--log-append','-la'), dict(
149 (('--log-append','-la'), dict(
149 type=unicode, dest='InteractiveShell.logappend', default=NoConfigDefault,
150 type=unicode, dest='InteractiveShell.logappend', default=NoConfigDefault,
150 help="Start logging to the give file in append mode.",
151 help="Start logging to the give file in append mode.",
151 metavar='InteractiveShell.logfile')
152 metavar='InteractiveShell.logfile')
152 ),
153 ),
153 (('--pdb',), dict(
154 (('--pdb',), dict(
154 action='store_true', dest='InteractiveShell.pdb', default=NoConfigDefault,
155 action='store_true', dest='InteractiveShell.pdb', default=NoConfigDefault,
155 help="Enable auto calling the pdb debugger after every exception.")
156 help="Enable auto calling the pdb debugger after every exception.")
156 ),
157 ),
157 (('--no-pdb',), dict(
158 (('--no-pdb',), dict(
158 action='store_false', dest='InteractiveShell.pdb', default=NoConfigDefault,
159 action='store_false', dest='InteractiveShell.pdb', default=NoConfigDefault,
159 help="Disable auto calling the pdb debugger after every exception.")
160 help="Disable auto calling the pdb debugger after every exception.")
160 ),
161 ),
161 (('--pprint',), dict(
162 (('--pprint',), dict(
162 action='store_true', dest='InteractiveShell.pprint', default=NoConfigDefault,
163 action='store_true', dest='InteractiveShell.pprint', default=NoConfigDefault,
163 help="Enable auto pretty printing of results.")
164 help="Enable auto pretty printing of results.")
164 ),
165 ),
165 (('--no-pprint',), dict(
166 (('--no-pprint',), dict(
166 action='store_false', dest='InteractiveShell.pprint', default=NoConfigDefault,
167 action='store_false', dest='InteractiveShell.pprint', default=NoConfigDefault,
167 help="Disable auto auto pretty printing of results.")
168 help="Disable auto auto pretty printing of results.")
168 ),
169 ),
169 (('--prompt-in1','-pi1'), dict(
170 (('--prompt-in1','-pi1'), dict(
170 type=str, dest='InteractiveShell.prompt_in1', default=NoConfigDefault,
171 type=str, dest='InteractiveShell.prompt_in1', default=NoConfigDefault,
171 help="Set the main input prompt ('In [\#]: ')",
172 help="Set the main input prompt ('In [\#]: ')",
172 metavar='InteractiveShell.prompt_in1')
173 metavar='InteractiveShell.prompt_in1')
173 ),
174 ),
174 (('--prompt-in2','-pi2'), dict(
175 (('--prompt-in2','-pi2'), dict(
175 type=str, dest='InteractiveShell.prompt_in2', default=NoConfigDefault,
176 type=str, dest='InteractiveShell.prompt_in2', default=NoConfigDefault,
176 help="Set the secondary input prompt (' .\D.: ')",
177 help="Set the secondary input prompt (' .\D.: ')",
177 metavar='InteractiveShell.prompt_in2')
178 metavar='InteractiveShell.prompt_in2')
178 ),
179 ),
179 (('--prompt-out','-po'), dict(
180 (('--prompt-out','-po'), dict(
180 type=str, dest='InteractiveShell.prompt_out', default=NoConfigDefault,
181 type=str, dest='InteractiveShell.prompt_out', default=NoConfigDefault,
181 help="Set the output prompt ('Out[\#]:')",
182 help="Set the output prompt ('Out[\#]:')",
182 metavar='InteractiveShell.prompt_out')
183 metavar='InteractiveShell.prompt_out')
183 ),
184 ),
184 (('--quick',), dict(
185 (('--quick',), dict(
185 action='store_true', dest='Global.quick', default=NoConfigDefault,
186 action='store_true', dest='Global.quick', default=NoConfigDefault,
186 help="Enable quick startup with no config files.")
187 help="Enable quick startup with no config files.")
187 ),
188 ),
188 (('--readline',), dict(
189 (('--readline',), dict(
189 action='store_true', dest='InteractiveShell.readline_use', default=NoConfigDefault,
190 action='store_true', dest='InteractiveShell.readline_use', default=NoConfigDefault,
190 help="Enable readline for command line usage.")
191 help="Enable readline for command line usage.")
191 ),
192 ),
192 (('--no-readline',), dict(
193 (('--no-readline',), dict(
193 action='store_false', dest='InteractiveShell.readline_use', default=NoConfigDefault,
194 action='store_false', dest='InteractiveShell.readline_use', default=NoConfigDefault,
194 help="Disable readline for command line usage.")
195 help="Disable readline for command line usage.")
195 ),
196 ),
196 (('--screen-length','-sl'), dict(
197 (('--screen-length','-sl'), dict(
197 type=int, dest='InteractiveShell.screen_length', default=NoConfigDefault,
198 type=int, dest='InteractiveShell.screen_length', default=NoConfigDefault,
198 help='Number of lines on screen, used to control printing of long strings.',
199 help='Number of lines on screen, used to control printing of long strings.',
199 metavar='InteractiveShell.screen_length')
200 metavar='InteractiveShell.screen_length')
200 ),
201 ),
201 (('--separate-in','-si'), dict(
202 (('--separate-in','-si'), dict(
202 type=str, dest='InteractiveShell.separate_in', default=NoConfigDefault,
203 type=str, dest='InteractiveShell.separate_in', default=NoConfigDefault,
203 help="Separator before input prompts. Default '\n'.",
204 help="Separator before input prompts. Default '\n'.",
204 metavar='InteractiveShell.separate_in')
205 metavar='InteractiveShell.separate_in')
205 ),
206 ),
206 (('--separate-out','-so'), dict(
207 (('--separate-out','-so'), dict(
207 type=str, dest='InteractiveShell.separate_out', default=NoConfigDefault,
208 type=str, dest='InteractiveShell.separate_out', default=NoConfigDefault,
208 help="Separator before output prompts. Default 0 (nothing).",
209 help="Separator before output prompts. Default 0 (nothing).",
209 metavar='InteractiveShell.separate_out')
210 metavar='InteractiveShell.separate_out')
210 ),
211 ),
211 (('--separate-out2','-so2'), dict(
212 (('--separate-out2','-so2'), dict(
212 type=str, dest='InteractiveShell.separate_out2', default=NoConfigDefault,
213 type=str, dest='InteractiveShell.separate_out2', default=NoConfigDefault,
213 help="Separator after output prompts. Default 0 (nonight).",
214 help="Separator after output prompts. Default 0 (nonight).",
214 metavar='InteractiveShell.separate_out2')
215 metavar='InteractiveShell.separate_out2')
215 ),
216 ),
216 (('-no-sep',), dict(
217 (('-no-sep',), dict(
217 action='store_true', dest='Global.nosep', default=NoConfigDefault,
218 action='store_true', dest='Global.nosep', default=NoConfigDefault,
218 help="Eliminate all spacing between prompts.")
219 help="Eliminate all spacing between prompts.")
219 ),
220 ),
220 (('--term-title',), dict(
221 (('--term-title',), dict(
221 action='store_true', dest='InteractiveShell.term_title', default=NoConfigDefault,
222 action='store_true', dest='InteractiveShell.term_title', default=NoConfigDefault,
222 help="Enable auto setting the terminal title.")
223 help="Enable auto setting the terminal title.")
223 ),
224 ),
224 (('--no-term-title',), dict(
225 (('--no-term-title',), dict(
225 action='store_false', dest='InteractiveShell.term_title', default=NoConfigDefault,
226 action='store_false', dest='InteractiveShell.term_title', default=NoConfigDefault,
226 help="Disable auto setting the terminal title.")
227 help="Disable auto setting the terminal title.")
227 ),
228 ),
228 (('--xmode',), dict(
229 (('--xmode',), dict(
229 type=str, dest='InteractiveShell.xmode', default=NoConfigDefault,
230 type=str, dest='InteractiveShell.xmode', default=NoConfigDefault,
230 help="Exception mode ('Plain','Context','Verbose')",
231 help="Exception mode ('Plain','Context','Verbose')",
231 metavar='InteractiveShell.xmode')
232 metavar='InteractiveShell.xmode')
232 ),
233 ),
233 (('--ext',), dict(
234 (('--ext',), dict(
234 type=str, dest='Global.extra_extension', default=NoConfigDefault,
235 type=str, dest='Global.extra_extension', default=NoConfigDefault,
235 help="The dotted module name of an IPython extension to load.",
236 help="The dotted module name of an IPython extension to load.",
236 metavar='Global.extra_extension')
237 metavar='Global.extra_extension')
237 ),
238 ),
238 (('-c',), dict(
239 (('-c',), dict(
239 type=str, dest='Global.code_to_run', default=NoConfigDefault,
240 type=str, dest='Global.code_to_run', default=NoConfigDefault,
240 help="Execute the given command string.",
241 help="Execute the given command string.",
241 metavar='Global.code_to_run')
242 metavar='Global.code_to_run')
242 ),
243 ),
243 (('-i',), dict(
244 (('-i',), dict(
244 action='store_true', dest='Global.force_interact', default=NoConfigDefault,
245 action='store_true', dest='Global.force_interact', default=NoConfigDefault,
245 help="If running code from the command line, become interactive afterwards.")
246 help="If running code from the command line, become interactive afterwards.")
246 ),
247 ),
247
248
248 # Options to start with GUI control enabled from the beginning
249 # Options to start with GUI control enabled from the beginning
249 (('--gui',), dict(
250 (('--gui',), dict(
250 type=str, dest='Global.gui', default=NoConfigDefault,
251 type=str, dest='Global.gui', default=NoConfigDefault,
251 help="Enable GUI event loop integration ('qt', 'wx', 'gtk').",
252 help="Enable GUI event loop integration ('qt', 'wx', 'gtk').",
252 metavar='gui-mode')
253 metavar='gui-mode')
253 ),
254 ),
254
255
255 (('--pylab','-pylab'), dict(
256 (('--pylab','-pylab'), dict(
256 type=str, dest='Global.pylab', default=NoConfigDefault,
257 type=str, dest='Global.pylab', default=NoConfigDefault,
257 nargs='?', const='auto', metavar='gui-mode',
258 nargs='?', const='auto', metavar='gui-mode',
258 help="Pre-load matplotlib and numpy for interactive use. "+
259 help="Pre-load matplotlib and numpy for interactive use. "+
259 "If no value is given, the gui backend is matplotlib's, else use "+
260 "If no value is given, the gui backend is matplotlib's, else use "+
260 "one of: ['tk', 'qt', 'wx', 'gtk'].")
261 "one of: ['tk', 'qt', 'wx', 'gtk'].")
261 ),
262 ),
262
263
263 # Legacy GUI options. Leave them in for backwards compatibility, but the
264 # Legacy GUI options. Leave them in for backwards compatibility, but the
264 # 'thread' names are really a misnomer now.
265 # 'thread' names are really a misnomer now.
265 (('--wthread','-wthread'), dict(
266 (('--wthread','-wthread'), dict(
266 action='store_true', dest='Global.wthread', default=NoConfigDefault,
267 action='store_true', dest='Global.wthread', default=NoConfigDefault,
267 help="Enable wxPython event loop integration "+
268 help="Enable wxPython event loop integration "+
268 "(DEPRECATED, use --gui wx)")
269 "(DEPRECATED, use --gui wx)")
269 ),
270 ),
270 (('--q4thread','--qthread','-q4thread','-qthread'), dict(
271 (('--q4thread','--qthread','-q4thread','-qthread'), dict(
271 action='store_true', dest='Global.q4thread', default=NoConfigDefault,
272 action='store_true', dest='Global.q4thread', default=NoConfigDefault,
272 help="Enable Qt4 event loop integration. Qt3 is no longer supported. "+
273 help="Enable Qt4 event loop integration. Qt3 is no longer supported. "+
273 "(DEPRECATED, use --gui qt)")
274 "(DEPRECATED, use --gui qt)")
274 ),
275 ),
275 (('--gthread','-gthread'), dict(
276 (('--gthread','-gthread'), dict(
276 action='store_true', dest='Global.gthread', default=NoConfigDefault,
277 action='store_true', dest='Global.gthread', default=NoConfigDefault,
277 help="Enable GTK event loop integration. "+
278 help="Enable GTK event loop integration. "+
278 "(DEPRECATED, use --gui gtk)")
279 "(DEPRECATED, use --gui gtk)")
279 ),
280 ),
280 )
281 )
281
282
282
283
283 class IPythonAppCLConfigLoader(BaseAppArgParseConfigLoader):
284
285 arguments = cl_args
286
287
288 default_config_file_name = u'ipython_config.py'
284 default_config_file_name = u'ipython_config.py'
289
285
290
291 class IPythonApp(Application):
286 class IPythonApp(Application):
292 name = u'ipython'
287 name = u'ipython'
293 description = 'IPython: an enhanced interactive Python shell.'
288 description = 'IPython: an enhanced interactive Python shell.'
294 config_file_name = default_config_file_name
289 config_file_name = default_config_file_name
295
290
291 cl_arguments = Application.cl_arguments + cl_args
292
293 # Private and configuration attributes
294 _CrashHandler = crashhandler.IPythonCrashHandler
295
296 def __init__(self, argv=None, **shell_params):
296 def __init__(self, argv=None, **shell_params):
297 """Create a new IPythonApp.
297 """Create a new IPythonApp.
298
298
299 Parameters
299 Parameters
300 ----------
300 ----------
301 argv : optional, list
301 argv : optional, list
302 If given, used as the command-line argv environment to read arguments
302 If given, used as the command-line argv environment to read arguments
303 from.
303 from.
304
304
305 shell_params : optional, dict
305 shell_params : optional, dict
306 All other keywords are passed to the :class:`iplib.InteractiveShell`
306 All other keywords are passed to the :class:`iplib.InteractiveShell`
307 constructor.
307 constructor.
308 """
308 """
309 super(IPythonApp, self).__init__(argv)
309 super(IPythonApp, self).__init__(argv)
310 self.shell_params = shell_params
310 self.shell_params = shell_params
311
311
312
312 def create_default_config(self):
313 def create_default_config(self):
313 super(IPythonApp, self).create_default_config()
314 super(IPythonApp, self).create_default_config()
314 # Eliminate multiple lookups
315 # Eliminate multiple lookups
315 Global = self.default_config.Global
316 Global = self.default_config.Global
316
317
317 # Set all default values
318 # Set all default values
318 Global.display_banner = True
319 Global.display_banner = True
319
320
320 # If the -c flag is given or a file is given to run at the cmd line
321 # If the -c flag is given or a file is given to run at the cmd line
321 # like "ipython foo.py", normally we exit without starting the main
322 # like "ipython foo.py", normally we exit without starting the main
322 # loop. The force_interact config variable allows a user to override
323 # loop. The force_interact config variable allows a user to override
323 # this and interact. It is also set by the -i cmd line flag, just
324 # this and interact. It is also set by the -i cmd line flag, just
324 # like Python.
325 # like Python.
325 Global.force_interact = False
326 Global.force_interact = False
326
327
327 # By default always interact by starting the IPython mainloop.
328 # By default always interact by starting the IPython mainloop.
328 Global.interact = True
329 Global.interact = True
329
330
330 # No GUI integration by default
331 # No GUI integration by default
331 Global.gui = False
332 Global.gui = False
332 # Pylab off by default
333 # Pylab off by default
333 Global.pylab = False
334 Global.pylab = False
334
335
335 # Deprecated versions of gui support that used threading, we support
336 # Deprecated versions of gui support that used threading, we support
336 # them just for bacwards compatibility as an alternate spelling for
337 # them just for bacwards compatibility as an alternate spelling for
337 # '--gui X'
338 # '--gui X'
338 Global.qthread = False
339 Global.qthread = False
339 Global.q4thread = False
340 Global.q4thread = False
340 Global.wthread = False
341 Global.wthread = False
341 Global.gthread = False
342 Global.gthread = False
342
343
343 def create_command_line_config(self):
344 """Create and return a command line config loader."""
345 return IPythonAppCLConfigLoader(self.argv,
346 description=self.description,
347 version=release.version
348 )
349
350 def load_file_config(self):
344 def load_file_config(self):
351 if hasattr(self.command_line_config.Global, 'quick'):
345 if hasattr(self.command_line_config.Global, 'quick'):
352 if self.command_line_config.Global.quick:
346 if self.command_line_config.Global.quick:
353 self.file_config = Config()
347 self.file_config = Config()
354 return
348 return
355 super(IPythonApp, self).load_file_config()
349 super(IPythonApp, self).load_file_config()
356
350
357 def post_load_file_config(self):
351 def post_load_file_config(self):
358 if hasattr(self.command_line_config.Global, 'extra_extension'):
352 if hasattr(self.command_line_config.Global, 'extra_extension'):
359 if not hasattr(self.file_config.Global, 'extensions'):
353 if not hasattr(self.file_config.Global, 'extensions'):
360 self.file_config.Global.extensions = []
354 self.file_config.Global.extensions = []
361 self.file_config.Global.extensions.append(
355 self.file_config.Global.extensions.append(
362 self.command_line_config.Global.extra_extension)
356 self.command_line_config.Global.extra_extension)
363 del self.command_line_config.Global.extra_extension
357 del self.command_line_config.Global.extra_extension
364
358
365 def pre_construct(self):
359 def pre_construct(self):
366 config = self.master_config
360 config = self.master_config
367
361
368 if hasattr(config.Global, 'classic'):
362 if hasattr(config.Global, 'classic'):
369 if config.Global.classic:
363 if config.Global.classic:
370 config.InteractiveShell.cache_size = 0
364 config.InteractiveShell.cache_size = 0
371 config.InteractiveShell.pprint = 0
365 config.InteractiveShell.pprint = 0
372 config.InteractiveShell.prompt_in1 = '>>> '
366 config.InteractiveShell.prompt_in1 = '>>> '
373 config.InteractiveShell.prompt_in2 = '... '
367 config.InteractiveShell.prompt_in2 = '... '
374 config.InteractiveShell.prompt_out = ''
368 config.InteractiveShell.prompt_out = ''
375 config.InteractiveShell.separate_in = \
369 config.InteractiveShell.separate_in = \
376 config.InteractiveShell.separate_out = \
370 config.InteractiveShell.separate_out = \
377 config.InteractiveShell.separate_out2 = ''
371 config.InteractiveShell.separate_out2 = ''
378 config.InteractiveShell.colors = 'NoColor'
372 config.InteractiveShell.colors = 'NoColor'
379 config.InteractiveShell.xmode = 'Plain'
373 config.InteractiveShell.xmode = 'Plain'
380
374
381 if hasattr(config.Global, 'nosep'):
375 if hasattr(config.Global, 'nosep'):
382 if config.Global.nosep:
376 if config.Global.nosep:
383 config.InteractiveShell.separate_in = \
377 config.InteractiveShell.separate_in = \
384 config.InteractiveShell.separate_out = \
378 config.InteractiveShell.separate_out = \
385 config.InteractiveShell.separate_out2 = ''
379 config.InteractiveShell.separate_out2 = ''
386
380
387 # if there is code of files to run from the cmd line, don't interact
381 # if there is code of files to run from the cmd line, don't interact
388 # unless the -i flag (Global.force_interact) is true.
382 # unless the -i flag (Global.force_interact) is true.
389 code_to_run = config.Global.get('code_to_run','')
383 code_to_run = config.Global.get('code_to_run','')
390 file_to_run = False
384 file_to_run = False
391 if len(self.extra_args)>=1:
385 if len(self.extra_args)>=1:
392 if self.extra_args[0]:
386 if self.extra_args[0]:
393 file_to_run = True
387 file_to_run = True
394 if file_to_run or code_to_run:
388 if file_to_run or code_to_run:
395 if not config.Global.force_interact:
389 if not config.Global.force_interact:
396 config.Global.interact = False
390 config.Global.interact = False
397
391
398 def construct(self):
392 def construct(self):
399 # I am a little hesitant to put these into InteractiveShell itself.
393 # I am a little hesitant to put these into InteractiveShell itself.
400 # But that might be the place for them
394 # But that might be the place for them
401 sys.path.insert(0, '')
395 sys.path.insert(0, '')
402
396
403 # Create an InteractiveShell instance
397 # Create an InteractiveShell instance
404 self.shell = InteractiveShell(None, self.master_config,
398 self.shell = InteractiveShell(None, self.master_config,
405 **self.shell_params )
399 **self.shell_params )
406
400
407 def post_construct(self):
401 def post_construct(self):
408 """Do actions after construct, but before starting the app."""
402 """Do actions after construct, but before starting the app."""
409 config = self.master_config
403 config = self.master_config
410
404
411 # shell.display_banner should always be False for the terminal
405 # shell.display_banner should always be False for the terminal
412 # based app, because we call shell.show_banner() by hand below
406 # based app, because we call shell.show_banner() by hand below
413 # so the banner shows *before* all extension loading stuff.
407 # so the banner shows *before* all extension loading stuff.
414 self.shell.display_banner = False
408 self.shell.display_banner = False
415
409
416 if config.Global.display_banner and \
410 if config.Global.display_banner and \
417 config.Global.interact:
411 config.Global.interact:
418 self.shell.show_banner()
412 self.shell.show_banner()
419
413
420 # Make sure there is a space below the banner.
414 # Make sure there is a space below the banner.
421 if self.log_level <= logging.INFO: print
415 if self.log_level <= logging.INFO: print
422
416
423 # Now a variety of things that happen after the banner is printed.
417 # Now a variety of things that happen after the banner is printed.
424 self._enable_gui_pylab()
418 self._enable_gui_pylab()
425 self._load_extensions()
419 self._load_extensions()
426 self._run_exec_lines()
420 self._run_exec_lines()
427 self._run_exec_files()
421 self._run_exec_files()
428 self._run_cmd_line_code()
422 self._run_cmd_line_code()
429 self._configure_xmode()
423 self._configure_xmode()
430
424
431 def _enable_gui_pylab(self):
425 def _enable_gui_pylab(self):
432 """Enable GUI event loop integration, taking pylab into account."""
426 """Enable GUI event loop integration, taking pylab into account."""
433 Global = self.master_config.Global
427 Global = self.master_config.Global
434
428
435 # Select which gui to use
429 # Select which gui to use
436 if Global.gui:
430 if Global.gui:
437 gui = Global.gui
431 gui = Global.gui
438 # The following are deprecated, but there's likely to be a lot of use
432 # The following are deprecated, but there's likely to be a lot of use
439 # of this form out there, so we might as well support it for now. But
433 # of this form out there, so we might as well support it for now. But
440 # the --gui option above takes precedence.
434 # the --gui option above takes precedence.
441 elif Global.wthread:
435 elif Global.wthread:
442 gui = inputhook.GUI_WX
436 gui = inputhook.GUI_WX
443 elif Global.qthread:
437 elif Global.qthread:
444 gui = inputhook.GUI_QT
438 gui = inputhook.GUI_QT
445 elif Global.gthread:
439 elif Global.gthread:
446 gui = inputhook.GUI_GTK
440 gui = inputhook.GUI_GTK
447 else:
441 else:
448 gui = None
442 gui = None
449
443
450 # Using --pylab will also require gui activation, though which toolkit
444 # Using --pylab will also require gui activation, though which toolkit
451 # to use may be chosen automatically based on mpl configuration.
445 # to use may be chosen automatically based on mpl configuration.
452 if Global.pylab:
446 if Global.pylab:
453 activate = self.shell.enable_pylab
447 activate = self.shell.enable_pylab
454 if Global.pylab == 'auto':
448 if Global.pylab == 'auto':
455 gui = None
449 gui = None
456 else:
450 else:
457 gui = Global.pylab
451 gui = Global.pylab
458 else:
452 else:
459 # Enable only GUI integration, no pylab
453 # Enable only GUI integration, no pylab
460 activate = inputhook.enable_gui
454 activate = inputhook.enable_gui
461
455
462 if gui or Global.pylab:
456 if gui or Global.pylab:
463 try:
457 try:
464 self.log.info("Enabling GUI event loop integration, "
458 self.log.info("Enabling GUI event loop integration, "
465 "toolkit=%s, pylab=%s" % (gui, Global.pylab) )
459 "toolkit=%s, pylab=%s" % (gui, Global.pylab) )
466 activate(gui)
460 activate(gui)
467 except:
461 except:
468 self.log.warn("Error in enabling GUI event loop integration:")
462 self.log.warn("Error in enabling GUI event loop integration:")
469 self.shell.showtraceback()
463 self.shell.showtraceback()
470
464
471 def _load_extensions(self):
465 def _load_extensions(self):
472 """Load all IPython extensions in Global.extensions.
466 """Load all IPython extensions in Global.extensions.
473
467
474 This uses the :meth:`InteractiveShell.load_extensions` to load all
468 This uses the :meth:`InteractiveShell.load_extensions` to load all
475 the extensions listed in ``self.master_config.Global.extensions``.
469 the extensions listed in ``self.master_config.Global.extensions``.
476 """
470 """
477 try:
471 try:
478 if hasattr(self.master_config.Global, 'extensions'):
472 if hasattr(self.master_config.Global, 'extensions'):
479 self.log.debug("Loading IPython extensions...")
473 self.log.debug("Loading IPython extensions...")
480 extensions = self.master_config.Global.extensions
474 extensions = self.master_config.Global.extensions
481 for ext in extensions:
475 for ext in extensions:
482 try:
476 try:
483 self.log.info("Loading IPython extension: %s" % ext)
477 self.log.info("Loading IPython extension: %s" % ext)
484 self.shell.load_extension(ext)
478 self.shell.load_extension(ext)
485 except:
479 except:
486 self.log.warn("Error in loading extension: %s" % ext)
480 self.log.warn("Error in loading extension: %s" % ext)
487 self.shell.showtraceback()
481 self.shell.showtraceback()
488 except:
482 except:
489 self.log.warn("Unknown error in loading extensions:")
483 self.log.warn("Unknown error in loading extensions:")
490 self.shell.showtraceback()
484 self.shell.showtraceback()
491
485
492 def _run_exec_lines(self):
486 def _run_exec_lines(self):
493 """Run lines of code in Global.exec_lines in the user's namespace."""
487 """Run lines of code in Global.exec_lines in the user's namespace."""
494 try:
488 try:
495 if hasattr(self.master_config.Global, 'exec_lines'):
489 if hasattr(self.master_config.Global, 'exec_lines'):
496 self.log.debug("Running code from Global.exec_lines...")
490 self.log.debug("Running code from Global.exec_lines...")
497 exec_lines = self.master_config.Global.exec_lines
491 exec_lines = self.master_config.Global.exec_lines
498 for line in exec_lines:
492 for line in exec_lines:
499 try:
493 try:
500 self.log.info("Running code in user namespace: %s" % line)
494 self.log.info("Running code in user namespace: %s" % line)
501 self.shell.runlines(line)
495 self.shell.runlines(line)
502 except:
496 except:
503 self.log.warn("Error in executing line in user namespace: %s" % line)
497 self.log.warn("Error in executing line in user namespace: %s" % line)
504 self.shell.showtraceback()
498 self.shell.showtraceback()
505 except:
499 except:
506 self.log.warn("Unknown error in handling Global.exec_lines:")
500 self.log.warn("Unknown error in handling Global.exec_lines:")
507 self.shell.showtraceback()
501 self.shell.showtraceback()
508
502
509 def _exec_file(self, fname):
503 def _exec_file(self, fname):
510 full_filename = filefind(fname, [u'.', self.ipython_dir])
504 full_filename = filefind(fname, [u'.', self.ipython_dir])
511 if os.path.isfile(full_filename):
505 if os.path.isfile(full_filename):
512 if full_filename.endswith(u'.py'):
506 if full_filename.endswith(u'.py'):
513 self.log.info("Running file in user namespace: %s" % full_filename)
507 self.log.info("Running file in user namespace: %s" % full_filename)
514 self.shell.safe_execfile(full_filename, self.shell.user_ns)
508 self.shell.safe_execfile(full_filename, self.shell.user_ns)
515 elif full_filename.endswith('.ipy'):
509 elif full_filename.endswith('.ipy'):
516 self.log.info("Running file in user namespace: %s" % full_filename)
510 self.log.info("Running file in user namespace: %s" % full_filename)
517 self.shell.safe_execfile_ipy(full_filename)
511 self.shell.safe_execfile_ipy(full_filename)
518 else:
512 else:
519 self.log.warn("File does not have a .py or .ipy extension: <%s>" % full_filename)
513 self.log.warn("File does not have a .py or .ipy extension: <%s>" % full_filename)
520
514
521 def _run_exec_files(self):
515 def _run_exec_files(self):
522 try:
516 try:
523 if hasattr(self.master_config.Global, 'exec_files'):
517 if hasattr(self.master_config.Global, 'exec_files'):
524 self.log.debug("Running files in Global.exec_files...")
518 self.log.debug("Running files in Global.exec_files...")
525 exec_files = self.master_config.Global.exec_files
519 exec_files = self.master_config.Global.exec_files
526 for fname in exec_files:
520 for fname in exec_files:
527 self._exec_file(fname)
521 self._exec_file(fname)
528 except:
522 except:
529 self.log.warn("Unknown error in handling Global.exec_files:")
523 self.log.warn("Unknown error in handling Global.exec_files:")
530 self.shell.showtraceback()
524 self.shell.showtraceback()
531
525
532 def _run_cmd_line_code(self):
526 def _run_cmd_line_code(self):
533 if hasattr(self.master_config.Global, 'code_to_run'):
527 if hasattr(self.master_config.Global, 'code_to_run'):
534 line = self.master_config.Global.code_to_run
528 line = self.master_config.Global.code_to_run
535 try:
529 try:
536 self.log.info("Running code given at command line (-c): %s" % line)
530 self.log.info("Running code given at command line (-c): %s" % line)
537 self.shell.runlines(line)
531 self.shell.runlines(line)
538 except:
532 except:
539 self.log.warn("Error in executing line in user namespace: %s" % line)
533 self.log.warn("Error in executing line in user namespace: %s" % line)
540 self.shell.showtraceback()
534 self.shell.showtraceback()
541 return
535 return
542 # Like Python itself, ignore the second if the first of these is present
536 # Like Python itself, ignore the second if the first of these is present
543 try:
537 try:
544 fname = self.extra_args[0]
538 fname = self.extra_args[0]
545 except:
539 except:
546 pass
540 pass
547 else:
541 else:
548 try:
542 try:
549 self._exec_file(fname)
543 self._exec_file(fname)
550 except:
544 except:
551 self.log.warn("Error in executing file in user namespace: %s" % fname)
545 self.log.warn("Error in executing file in user namespace: %s" % fname)
552 self.shell.showtraceback()
546 self.shell.showtraceback()
553
547
554 def _configure_xmode(self):
548 def _configure_xmode(self):
555 # XXX - shouldn't this be read from the config? I'm still a little
549 # XXX - shouldn't this be read from the config? I'm still a little
556 # lost with all the details of handling the new config guys...
550 # lost with all the details of handling the new config guys...
557 self.shell.InteractiveTB.set_mode(mode=self.shell.xmode)
551 self.shell.InteractiveTB.set_mode(mode=self.shell.xmode)
558
552
559 def start_app(self):
553 def start_app(self):
560 if self.master_config.Global.interact:
554 if self.master_config.Global.interact:
561 self.log.debug("Starting IPython's mainloop...")
555 self.log.debug("Starting IPython's mainloop...")
562 self.shell.mainloop()
556 self.shell.mainloop()
563 else:
557 else:
564 self.log.debug("IPython not interactive, start_app is no-op...")
558 self.log.debug("IPython not interactive, start_app is no-op...")
565
559
566
560
567 def load_default_config(ipython_dir=None):
561 def load_default_config(ipython_dir=None):
568 """Load the default config file from the default ipython_dir.
562 """Load the default config file from the default ipython_dir.
569
563
570 This is useful for embedded shells.
564 This is useful for embedded shells.
571 """
565 """
572 if ipython_dir is None:
566 if ipython_dir is None:
573 ipython_dir = get_ipython_dir()
567 ipython_dir = get_ipython_dir()
574 cl = PyFileConfigLoader(default_config_file_name, ipython_dir)
568 cl = PyFileConfigLoader(default_config_file_name, ipython_dir)
575 config = cl.load_config()
569 config = cl.load_config()
576 return config
570 return config
577
571
578
572
579 def launch_new_instance():
573 def launch_new_instance():
580 """Create and run a full blown IPython instance"""
574 """Create and run a full blown IPython instance"""
581 app = IPythonApp()
575 app = IPythonApp()
582 app.start()
576 app.start()
@@ -1,2544 +1,2521 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 Main IPython Component
3 Main IPython Component
4 """
4 """
5
5
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
8 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
8 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
9 # Copyright (C) 2008-2009 The IPython Development Team
9 # Copyright (C) 2008-2009 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 from __future__ import with_statement
19 from __future__ import with_statement
20
20
21 import __builtin__
21 import __builtin__
22 import StringIO
22 import StringIO
23 import bdb
23 import bdb
24 import codeop
24 import codeop
25 import exceptions
25 import exceptions
26 import new
26 import new
27 import os
27 import os
28 import re
28 import re
29 import string
29 import string
30 import sys
30 import sys
31 import tempfile
31 import tempfile
32 from contextlib import nested
32 from contextlib import nested
33
33
34 from IPython.core import debugger, oinspect
34 from IPython.core import debugger, oinspect
35 from IPython.core import history as ipcorehist
35 from IPython.core import history as ipcorehist
36 from IPython.core import prefilter
36 from IPython.core import prefilter
37 from IPython.core import shadowns
37 from IPython.core import shadowns
38 from IPython.core import ultratb
38 from IPython.core import ultratb
39 from IPython.core.alias import AliasManager
39 from IPython.core.alias import AliasManager
40 from IPython.core.builtin_trap import BuiltinTrap
40 from IPython.core.builtin_trap import BuiltinTrap
41 from IPython.core.component import Component
41 from IPython.core.component import Component
42 from IPython.core.display_trap import DisplayTrap
42 from IPython.core.display_trap import DisplayTrap
43 from IPython.core.error import TryNext, UsageError
43 from IPython.core.error import TryNext, UsageError
44 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
44 from IPython.core.fakemodule import FakeModule, init_fakemod_dict
45 from IPython.core.logger import Logger
45 from IPython.core.logger import Logger
46 from IPython.core.magic import Magic
46 from IPython.core.magic import Magic
47 from IPython.core.prefilter import PrefilterManager
47 from IPython.core.prefilter import PrefilterManager
48 from IPython.core.prompts import CachedOutput
48 from IPython.core.prompts import CachedOutput
49 from IPython.core.pylabtools import pylab_activate
49 from IPython.core.pylabtools import pylab_activate
50 from IPython.core.usage import interactive_usage, default_banner
50 from IPython.core.usage import interactive_usage, default_banner
51 from IPython.external.Itpl import ItplNS
51 from IPython.external.Itpl import ItplNS
52 from IPython.lib.inputhook import enable_gui
52 from IPython.lib.inputhook import enable_gui
53 from IPython.lib.backgroundjobs import BackgroundJobManager
53 from IPython.lib.backgroundjobs import BackgroundJobManager
54 from IPython.utils import PyColorize
54 from IPython.utils import PyColorize
55 from IPython.utils import pickleshare
55 from IPython.utils import pickleshare
56 from IPython.utils.genutils import get_ipython_dir
56 from IPython.utils.genutils import get_ipython_dir
57 from IPython.utils.ipstruct import Struct
57 from IPython.utils.ipstruct import Struct
58 from IPython.utils.platutils import toggle_set_term_title, set_term_title
58 from IPython.utils.platutils import toggle_set_term_title, set_term_title
59 from IPython.utils.strdispatch import StrDispatch
59 from IPython.utils.strdispatch import StrDispatch
60 from IPython.utils.syspathcontext import prepended_to_syspath
60 from IPython.utils.syspathcontext import prepended_to_syspath
61
61
62 # XXX - need to clean up this import * line
62 # XXX - need to clean up this import * line
63 from IPython.utils.genutils import *
63 from IPython.utils.genutils import *
64
64
65 # from IPython.utils import growl
65 # from IPython.utils import growl
66 # growl.start("IPython")
66 # growl.start("IPython")
67
67
68 from IPython.utils.traitlets import (
68 from IPython.utils.traitlets import (
69 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode
69 Int, Str, CBool, CaselessStrEnum, Enum, List, Unicode
70 )
70 )
71
71
72 #-----------------------------------------------------------------------------
72 #-----------------------------------------------------------------------------
73 # Globals
73 # Globals
74 #-----------------------------------------------------------------------------
74 #-----------------------------------------------------------------------------
75
75
76 # store the builtin raw_input globally, and use this always, in case user code
76 # store the builtin raw_input globally, and use this always, in case user code
77 # overwrites it (like wx.py.PyShell does)
77 # overwrites it (like wx.py.PyShell does)
78 raw_input_original = raw_input
78 raw_input_original = raw_input
79
79
80 # compiled regexps for autoindent management
80 # compiled regexps for autoindent management
81 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
81 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
82
82
83 #-----------------------------------------------------------------------------
83 #-----------------------------------------------------------------------------
84 # Utilities
84 # Utilities
85 #-----------------------------------------------------------------------------
85 #-----------------------------------------------------------------------------
86
86
87 ini_spaces_re = re.compile(r'^(\s+)')
87 ini_spaces_re = re.compile(r'^(\s+)')
88
88
89
89
90 def num_ini_spaces(strng):
90 def num_ini_spaces(strng):
91 """Return the number of initial spaces in a string"""
91 """Return the number of initial spaces in a string"""
92
92
93 ini_spaces = ini_spaces_re.match(strng)
93 ini_spaces = ini_spaces_re.match(strng)
94 if ini_spaces:
94 if ini_spaces:
95 return ini_spaces.end()
95 return ini_spaces.end()
96 else:
96 else:
97 return 0
97 return 0
98
98
99
99
100 def softspace(file, newvalue):
100 def softspace(file, newvalue):
101 """Copied from code.py, to remove the dependency"""
101 """Copied from code.py, to remove the dependency"""
102
102
103 oldvalue = 0
103 oldvalue = 0
104 try:
104 try:
105 oldvalue = file.softspace
105 oldvalue = file.softspace
106 except AttributeError:
106 except AttributeError:
107 pass
107 pass
108 try:
108 try:
109 file.softspace = newvalue
109 file.softspace = newvalue
110 except (AttributeError, TypeError):
110 except (AttributeError, TypeError):
111 # "attribute-less object" or "read-only attributes"
111 # "attribute-less object" or "read-only attributes"
112 pass
112 pass
113 return oldvalue
113 return oldvalue
114
114
115
115
116 def no_op(*a, **kw): pass
116 def no_op(*a, **kw): pass
117
117
118 class SpaceInInput(exceptions.Exception): pass
118 class SpaceInInput(exceptions.Exception): pass
119
119
120 class Bunch: pass
120 class Bunch: pass
121
121
122 class InputList(list):
122 class InputList(list):
123 """Class to store user input.
123 """Class to store user input.
124
124
125 It's basically a list, but slices return a string instead of a list, thus
125 It's basically a list, but slices return a string instead of a list, thus
126 allowing things like (assuming 'In' is an instance):
126 allowing things like (assuming 'In' is an instance):
127
127
128 exec In[4:7]
128 exec In[4:7]
129
129
130 or
130 or
131
131
132 exec In[5:9] + In[14] + In[21:25]"""
132 exec In[5:9] + In[14] + In[21:25]"""
133
133
134 def __getslice__(self,i,j):
134 def __getslice__(self,i,j):
135 return ''.join(list.__getslice__(self,i,j))
135 return ''.join(list.__getslice__(self,i,j))
136
136
137
137
138 class SyntaxTB(ultratb.ListTB):
138 class SyntaxTB(ultratb.ListTB):
139 """Extension which holds some state: the last exception value"""
139 """Extension which holds some state: the last exception value"""
140
140
141 def __init__(self,color_scheme = 'NoColor'):
141 def __init__(self,color_scheme = 'NoColor'):
142 ultratb.ListTB.__init__(self,color_scheme)
142 ultratb.ListTB.__init__(self,color_scheme)
143 self.last_syntax_error = None
143 self.last_syntax_error = None
144
144
145 def __call__(self, etype, value, elist):
145 def __call__(self, etype, value, elist):
146 self.last_syntax_error = value
146 self.last_syntax_error = value
147 ultratb.ListTB.__call__(self,etype,value,elist)
147 ultratb.ListTB.__call__(self,etype,value,elist)
148
148
149 def clear_err_state(self):
149 def clear_err_state(self):
150 """Return the current error state and clear it"""
150 """Return the current error state and clear it"""
151 e = self.last_syntax_error
151 e = self.last_syntax_error
152 self.last_syntax_error = None
152 self.last_syntax_error = None
153 return e
153 return e
154
154
155
155
156 def get_default_editor():
156 def get_default_editor():
157 try:
157 try:
158 ed = os.environ['EDITOR']
158 ed = os.environ['EDITOR']
159 except KeyError:
159 except KeyError:
160 if os.name == 'posix':
160 if os.name == 'posix':
161 ed = 'vi' # the only one guaranteed to be there!
161 ed = 'vi' # the only one guaranteed to be there!
162 else:
162 else:
163 ed = 'notepad' # same in Windows!
163 ed = 'notepad' # same in Windows!
164 return ed
164 return ed
165
165
166
166
167 def get_default_colors():
167 def get_default_colors():
168 if sys.platform=='darwin':
168 if sys.platform=='darwin':
169 return "LightBG"
169 return "LightBG"
170 elif os.name=='nt':
170 elif os.name=='nt':
171 return 'Linux'
171 return 'Linux'
172 else:
172 else:
173 return 'Linux'
173 return 'Linux'
174
174
175
175
176 class SeparateStr(Str):
176 class SeparateStr(Str):
177 """A Str subclass to validate separate_in, separate_out, etc.
177 """A Str subclass to validate separate_in, separate_out, etc.
178
178
179 This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'.
179 This is a Str based traitlet that converts '0'->'' and '\\n'->'\n'.
180 """
180 """
181
181
182 def validate(self, obj, value):
182 def validate(self, obj, value):
183 if value == '0': value = ''
183 if value == '0': value = ''
184 value = value.replace('\\n','\n')
184 value = value.replace('\\n','\n')
185 return super(SeparateStr, self).validate(obj, value)
185 return super(SeparateStr, self).validate(obj, value)
186
186
187
187
188 def make_user_namespaces(user_ns=None, user_global_ns=None):
188 def make_user_namespaces(user_ns=None, user_global_ns=None):
189 """Return a valid local and global user interactive namespaces.
189 """Return a valid local and global user interactive namespaces.
190
190
191 This builds a dict with the minimal information needed to operate as a
191 This builds a dict with the minimal information needed to operate as a
192 valid IPython user namespace, which you can pass to the various
192 valid IPython user namespace, which you can pass to the various
193 embedding classes in ipython. The default implementation returns the
193 embedding classes in ipython. The default implementation returns the
194 same dict for both the locals and the globals to allow functions to
194 same dict for both the locals and the globals to allow functions to
195 refer to variables in the namespace. Customized implementations can
195 refer to variables in the namespace. Customized implementations can
196 return different dicts. The locals dictionary can actually be anything
196 return different dicts. The locals dictionary can actually be anything
197 following the basic mapping protocol of a dict, but the globals dict
197 following the basic mapping protocol of a dict, but the globals dict
198 must be a true dict, not even a subclass. It is recommended that any
198 must be a true dict, not even a subclass. It is recommended that any
199 custom object for the locals namespace synchronize with the globals
199 custom object for the locals namespace synchronize with the globals
200 dict somehow.
200 dict somehow.
201
201
202 Raises TypeError if the provided globals namespace is not a true dict.
202 Raises TypeError if the provided globals namespace is not a true dict.
203
203
204 Parameters
204 Parameters
205 ----------
205 ----------
206 user_ns : dict-like, optional
206 user_ns : dict-like, optional
207 The current user namespace. The items in this namespace should
207 The current user namespace. The items in this namespace should
208 be included in the output. If None, an appropriate blank
208 be included in the output. If None, an appropriate blank
209 namespace should be created.
209 namespace should be created.
210 user_global_ns : dict, optional
210 user_global_ns : dict, optional
211 The current user global namespace. The items in this namespace
211 The current user global namespace. The items in this namespace
212 should be included in the output. If None, an appropriate
212 should be included in the output. If None, an appropriate
213 blank namespace should be created.
213 blank namespace should be created.
214
214
215 Returns
215 Returns
216 -------
216 -------
217 A pair of dictionary-like object to be used as the local namespace
217 A pair of dictionary-like object to be used as the local namespace
218 of the interpreter and a dict to be used as the global namespace.
218 of the interpreter and a dict to be used as the global namespace.
219 """
219 """
220
220
221 if user_ns is None:
221 if user_ns is None:
222 # Set __name__ to __main__ to better match the behavior of the
222 # Set __name__ to __main__ to better match the behavior of the
223 # normal interpreter.
223 # normal interpreter.
224 user_ns = {'__name__' :'__main__',
224 user_ns = {'__name__' :'__main__',
225 '__builtins__' : __builtin__,
225 '__builtins__' : __builtin__,
226 }
226 }
227 else:
227 else:
228 user_ns.setdefault('__name__','__main__')
228 user_ns.setdefault('__name__','__main__')
229 user_ns.setdefault('__builtins__',__builtin__)
229 user_ns.setdefault('__builtins__',__builtin__)
230
230
231 if user_global_ns is None:
231 if user_global_ns is None:
232 user_global_ns = user_ns
232 user_global_ns = user_ns
233 if type(user_global_ns) is not dict:
233 if type(user_global_ns) is not dict:
234 raise TypeError("user_global_ns must be a true dict; got %r"
234 raise TypeError("user_global_ns must be a true dict; got %r"
235 % type(user_global_ns))
235 % type(user_global_ns))
236
236
237 return user_ns, user_global_ns
237 return user_ns, user_global_ns
238
238
239 #-----------------------------------------------------------------------------
239 #-----------------------------------------------------------------------------
240 # Main IPython class
240 # Main IPython class
241 #-----------------------------------------------------------------------------
241 #-----------------------------------------------------------------------------
242
242
243
243
244 class InteractiveShell(Component, Magic):
244 class InteractiveShell(Component, Magic):
245 """An enhanced, interactive shell for Python."""
245 """An enhanced, interactive shell for Python."""
246
246
247 autocall = Enum((0,1,2), default_value=1, config=True)
247 autocall = Enum((0,1,2), default_value=1, config=True)
248 autoedit_syntax = CBool(False, config=True)
248 autoedit_syntax = CBool(False, config=True)
249 autoindent = CBool(True, config=True)
249 autoindent = CBool(True, config=True)
250 automagic = CBool(True, config=True)
250 automagic = CBool(True, config=True)
251 banner = Str('')
251 banner = Str('')
252 banner1 = Str(default_banner, config=True)
252 banner1 = Str(default_banner, config=True)
253 banner2 = Str('', config=True)
253 banner2 = Str('', config=True)
254 cache_size = Int(1000, config=True)
254 cache_size = Int(1000, config=True)
255 color_info = CBool(True, config=True)
255 color_info = CBool(True, config=True)
256 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
256 colors = CaselessStrEnum(('NoColor','LightBG','Linux'),
257 default_value=get_default_colors(), config=True)
257 default_value=get_default_colors(), config=True)
258 confirm_exit = CBool(True, config=True)
258 confirm_exit = CBool(True, config=True)
259 debug = CBool(False, config=True)
259 debug = CBool(False, config=True)
260 deep_reload = CBool(False, config=True)
260 deep_reload = CBool(False, config=True)
261 # This display_banner only controls whether or not self.show_banner()
261 # This display_banner only controls whether or not self.show_banner()
262 # is called when mainloop/interact are called. The default is False
262 # is called when mainloop/interact are called. The default is False
263 # because for the terminal based application, the banner behavior
263 # because for the terminal based application, the banner behavior
264 # is controlled by Global.display_banner, which IPythonApp looks at
264 # is controlled by Global.display_banner, which IPythonApp looks at
265 # to determine if *it* should call show_banner() by hand or not.
265 # to determine if *it* should call show_banner() by hand or not.
266 display_banner = CBool(False) # This isn't configurable!
266 display_banner = CBool(False) # This isn't configurable!
267 embedded = CBool(False)
267 embedded = CBool(False)
268 embedded_active = CBool(False)
268 embedded_active = CBool(False)
269 editor = Str(get_default_editor(), config=True)
269 editor = Str(get_default_editor(), config=True)
270 filename = Str("<ipython console>")
270 filename = Str("<ipython console>")
271 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
271 ipython_dir= Unicode('', config=True) # Set to get_ipython_dir() in __init__
272 logstart = CBool(False, config=True)
272 logstart = CBool(False, config=True)
273 logfile = Str('', config=True)
273 logfile = Str('', config=True)
274 logappend = Str('', config=True)
274 logappend = Str('', config=True)
275 object_info_string_level = Enum((0,1,2), default_value=0,
275 object_info_string_level = Enum((0,1,2), default_value=0,
276 config=True)
276 config=True)
277 pager = Str('less', config=True)
277 pager = Str('less', config=True)
278 pdb = CBool(False, config=True)
278 pdb = CBool(False, config=True)
279 pprint = CBool(True, config=True)
279 pprint = CBool(True, config=True)
280 profile = Str('', config=True)
280 profile = Str('', config=True)
281 prompt_in1 = Str('In [\\#]: ', config=True)
281 prompt_in1 = Str('In [\\#]: ', config=True)
282 prompt_in2 = Str(' .\\D.: ', config=True)
282 prompt_in2 = Str(' .\\D.: ', config=True)
283 prompt_out = Str('Out[\\#]: ', config=True)
283 prompt_out = Str('Out[\\#]: ', config=True)
284 prompts_pad_left = CBool(True, config=True)
284 prompts_pad_left = CBool(True, config=True)
285 quiet = CBool(False, config=True)
285 quiet = CBool(False, config=True)
286
286
287 readline_use = CBool(True, config=True)
287 readline_use = CBool(True, config=True)
288 readline_merge_completions = CBool(True, config=True)
288 readline_merge_completions = CBool(True, config=True)
289 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
289 readline_omit__names = Enum((0,1,2), default_value=0, config=True)
290 readline_remove_delims = Str('-/~', config=True)
290 readline_remove_delims = Str('-/~', config=True)
291 readline_parse_and_bind = List([
291 readline_parse_and_bind = List([
292 'tab: complete',
292 'tab: complete',
293 '"\C-l": possible-completions',
293 '"\C-l": possible-completions',
294 'set show-all-if-ambiguous on',
294 'set show-all-if-ambiguous on',
295 '"\C-o": tab-insert',
295 '"\C-o": tab-insert',
296 '"\M-i": " "',
296 '"\M-i": " "',
297 '"\M-o": "\d\d\d\d"',
297 '"\M-o": "\d\d\d\d"',
298 '"\M-I": "\d\d\d\d"',
298 '"\M-I": "\d\d\d\d"',
299 '"\C-r": reverse-search-history',
299 '"\C-r": reverse-search-history',
300 '"\C-s": forward-search-history',
300 '"\C-s": forward-search-history',
301 '"\C-p": history-search-backward',
301 '"\C-p": history-search-backward',
302 '"\C-n": history-search-forward',
302 '"\C-n": history-search-forward',
303 '"\e[A": history-search-backward',
303 '"\e[A": history-search-backward',
304 '"\e[B": history-search-forward',
304 '"\e[B": history-search-forward',
305 '"\C-k": kill-line',
305 '"\C-k": kill-line',
306 '"\C-u": unix-line-discard',
306 '"\C-u": unix-line-discard',
307 ], allow_none=False, config=True)
307 ], allow_none=False, config=True)
308
308
309 screen_length = Int(0, config=True)
309 screen_length = Int(0, config=True)
310
310
311 # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n'
311 # Use custom TraitletTypes that convert '0'->'' and '\\n'->'\n'
312 separate_in = SeparateStr('\n', config=True)
312 separate_in = SeparateStr('\n', config=True)
313 separate_out = SeparateStr('', config=True)
313 separate_out = SeparateStr('', config=True)
314 separate_out2 = SeparateStr('', config=True)
314 separate_out2 = SeparateStr('', config=True)
315
315
316 system_header = Str('IPython system call: ', config=True)
316 system_header = Str('IPython system call: ', config=True)
317 system_verbose = CBool(False, config=True)
317 system_verbose = CBool(False, config=True)
318 term_title = CBool(False, config=True)
318 term_title = CBool(False, config=True)
319 wildcards_case_sensitive = CBool(True, config=True)
319 wildcards_case_sensitive = CBool(True, config=True)
320 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
320 xmode = CaselessStrEnum(('Context','Plain', 'Verbose'),
321 default_value='Context', config=True)
321 default_value='Context', config=True)
322
322
323 autoexec = List(allow_none=False)
323 autoexec = List(allow_none=False)
324
324
325 # class attribute to indicate whether the class supports threads or not.
325 # class attribute to indicate whether the class supports threads or not.
326 # Subclasses with thread support should override this as needed.
326 # Subclasses with thread support should override this as needed.
327 isthreaded = False
327 isthreaded = False
328
328
329 def __init__(self, parent=None, config=None, ipython_dir=None, usage=None,
329 def __init__(self, parent=None, config=None, ipython_dir=None, usage=None,
330 user_ns=None, user_global_ns=None,
330 user_ns=None, user_global_ns=None,
331 banner1=None, banner2=None, display_banner=None,
331 banner1=None, banner2=None, display_banner=None,
332 custom_exceptions=((),None)):
332 custom_exceptions=((),None)):
333
333
334 # This is where traitlets with a config_key argument are updated
334 # This is where traitlets with a config_key argument are updated
335 # from the values on config.
335 # from the values on config.
336 super(InteractiveShell, self).__init__(parent, config=config)
336 super(InteractiveShell, self).__init__(parent, config=config)
337
337
338 # These are relatively independent and stateless
338 # These are relatively independent and stateless
339 self.init_ipython_dir(ipython_dir)
339 self.init_ipython_dir(ipython_dir)
340 self.init_instance_attrs()
340 self.init_instance_attrs()
341 self.init_term_title()
341 self.init_term_title()
342 self.init_usage(usage)
342 self.init_usage(usage)
343 self.init_banner(banner1, banner2, display_banner)
343 self.init_banner(banner1, banner2, display_banner)
344
344
345 # Create namespaces (user_ns, user_global_ns, etc.)
345 # Create namespaces (user_ns, user_global_ns, etc.)
346 self.init_create_namespaces(user_ns, user_global_ns)
346 self.init_create_namespaces(user_ns, user_global_ns)
347 # This has to be done after init_create_namespaces because it uses
347 # This has to be done after init_create_namespaces because it uses
348 # something in self.user_ns, but before init_sys_modules, which
348 # something in self.user_ns, but before init_sys_modules, which
349 # is the first thing to modify sys.
349 # is the first thing to modify sys.
350 self.save_sys_module_state()
350 self.save_sys_module_state()
351 self.init_sys_modules()
351 self.init_sys_modules()
352
352
353 self.init_history()
353 self.init_history()
354 self.init_encoding()
354 self.init_encoding()
355 self.init_prefilter()
355 self.init_prefilter()
356
356
357 Magic.__init__(self, self)
357 Magic.__init__(self, self)
358
358
359 self.init_syntax_highlighting()
359 self.init_syntax_highlighting()
360 self.init_hooks()
360 self.init_hooks()
361 self.init_pushd_popd_magic()
361 self.init_pushd_popd_magic()
362 self.init_traceback_handlers(custom_exceptions)
362 self.init_traceback_handlers(custom_exceptions)
363 self.init_user_ns()
363 self.init_user_ns()
364 self.init_logger()
364 self.init_logger()
365 self.init_alias()
365 self.init_alias()
366 self.init_builtins()
366 self.init_builtins()
367
367
368 # pre_config_initialization
368 # pre_config_initialization
369 self.init_shadow_hist()
369 self.init_shadow_hist()
370
370
371 # The next section should contain averything that was in ipmaker.
371 # The next section should contain averything that was in ipmaker.
372 self.init_logstart()
372 self.init_logstart()
373
373
374 # The following was in post_config_initialization
374 # The following was in post_config_initialization
375 self.init_inspector()
375 self.init_inspector()
376 self.init_readline()
376 self.init_readline()
377 self.init_prompts()
377 self.init_prompts()
378 self.init_displayhook()
378 self.init_displayhook()
379 self.init_reload_doctest()
379 self.init_reload_doctest()
380 self.init_magics()
380 self.init_magics()
381 self.init_pdb()
381 self.init_pdb()
382 self.hooks.late_startup_hook()
382 self.hooks.late_startup_hook()
383
383
384 def get_ipython(self):
384 def get_ipython(self):
385 """Return the currently running IPython instance."""
385 """Return the currently running IPython instance."""
386 return self
386 return self
387
387
388 #-------------------------------------------------------------------------
388 #-------------------------------------------------------------------------
389 # Traitlet changed handlers
389 # Traitlet changed handlers
390 #-------------------------------------------------------------------------
390 #-------------------------------------------------------------------------
391
391
392 def _banner1_changed(self):
392 def _banner1_changed(self):
393 self.compute_banner()
393 self.compute_banner()
394
394
395 def _banner2_changed(self):
395 def _banner2_changed(self):
396 self.compute_banner()
396 self.compute_banner()
397
397
398 def _ipython_dir_changed(self, name, new):
398 def _ipython_dir_changed(self, name, new):
399 if not os.path.isdir(new):
399 if not os.path.isdir(new):
400 os.makedirs(new, mode = 0777)
400 os.makedirs(new, mode = 0777)
401 if not os.path.isdir(self.ipython_extension_dir):
401 if not os.path.isdir(self.ipython_extension_dir):
402 os.makedirs(self.ipython_extension_dir, mode = 0777)
402 os.makedirs(self.ipython_extension_dir, mode = 0777)
403
403
404 @property
404 @property
405 def ipython_extension_dir(self):
405 def ipython_extension_dir(self):
406 return os.path.join(self.ipython_dir, 'extensions')
406 return os.path.join(self.ipython_dir, 'extensions')
407
407
408 @property
408 @property
409 def usable_screen_length(self):
409 def usable_screen_length(self):
410 if self.screen_length == 0:
410 if self.screen_length == 0:
411 return 0
411 return 0
412 else:
412 else:
413 num_lines_bot = self.separate_in.count('\n')+1
413 num_lines_bot = self.separate_in.count('\n')+1
414 return self.screen_length - num_lines_bot
414 return self.screen_length - num_lines_bot
415
415
416 def _term_title_changed(self, name, new_value):
416 def _term_title_changed(self, name, new_value):
417 self.init_term_title()
417 self.init_term_title()
418
418
419 def set_autoindent(self,value=None):
419 def set_autoindent(self,value=None):
420 """Set the autoindent flag, checking for readline support.
420 """Set the autoindent flag, checking for readline support.
421
421
422 If called with no arguments, it acts as a toggle."""
422 If called with no arguments, it acts as a toggle."""
423
423
424 if not self.has_readline:
424 if not self.has_readline:
425 if os.name == 'posix':
425 if os.name == 'posix':
426 warn("The auto-indent feature requires the readline library")
426 warn("The auto-indent feature requires the readline library")
427 self.autoindent = 0
427 self.autoindent = 0
428 return
428 return
429 if value is None:
429 if value is None:
430 self.autoindent = not self.autoindent
430 self.autoindent = not self.autoindent
431 else:
431 else:
432 self.autoindent = value
432 self.autoindent = value
433
433
434 #-------------------------------------------------------------------------
434 #-------------------------------------------------------------------------
435 # init_* methods called by __init__
435 # init_* methods called by __init__
436 #-------------------------------------------------------------------------
436 #-------------------------------------------------------------------------
437
437
438 def init_ipython_dir(self, ipython_dir):
438 def init_ipython_dir(self, ipython_dir):
439 if ipython_dir is not None:
439 if ipython_dir is not None:
440 self.ipython_dir = ipython_dir
440 self.ipython_dir = ipython_dir
441 self.config.Global.ipython_dir = self.ipython_dir
441 self.config.Global.ipython_dir = self.ipython_dir
442 return
442 return
443
443
444 if hasattr(self.config.Global, 'ipython_dir'):
444 if hasattr(self.config.Global, 'ipython_dir'):
445 self.ipython_dir = self.config.Global.ipython_dir
445 self.ipython_dir = self.config.Global.ipython_dir
446 else:
446 else:
447 self.ipython_dir = get_ipython_dir()
447 self.ipython_dir = get_ipython_dir()
448
448
449 # All children can just read this
449 # All children can just read this
450 self.config.Global.ipython_dir = self.ipython_dir
450 self.config.Global.ipython_dir = self.ipython_dir
451
451
452 def init_instance_attrs(self):
452 def init_instance_attrs(self):
453 self.jobs = BackgroundJobManager()
453 self.jobs = BackgroundJobManager()
454 self.more = False
454 self.more = False
455
455
456 # command compiler
456 # command compiler
457 self.compile = codeop.CommandCompiler()
457 self.compile = codeop.CommandCompiler()
458
458
459 # User input buffer
459 # User input buffer
460 self.buffer = []
460 self.buffer = []
461
461
462 # Make an empty namespace, which extension writers can rely on both
462 # Make an empty namespace, which extension writers can rely on both
463 # existing and NEVER being used by ipython itself. This gives them a
463 # existing and NEVER being used by ipython itself. This gives them a
464 # convenient location for storing additional information and state
464 # convenient location for storing additional information and state
465 # their extensions may require, without fear of collisions with other
465 # their extensions may require, without fear of collisions with other
466 # ipython names that may develop later.
466 # ipython names that may develop later.
467 self.meta = Struct()
467 self.meta = Struct()
468
468
469 # Object variable to store code object waiting execution. This is
469 # Object variable to store code object waiting execution. This is
470 # used mainly by the multithreaded shells, but it can come in handy in
470 # used mainly by the multithreaded shells, but it can come in handy in
471 # other situations. No need to use a Queue here, since it's a single
471 # other situations. No need to use a Queue here, since it's a single
472 # item which gets cleared once run.
472 # item which gets cleared once run.
473 self.code_to_run = None
473 self.code_to_run = None
474
474
475 # Flag to mark unconditional exit
475 # Flag to mark unconditional exit
476 self.exit_now = False
476 self.exit_now = False
477
477
478 # Temporary files used for various purposes. Deleted at exit.
478 # Temporary files used for various purposes. Deleted at exit.
479 self.tempfiles = []
479 self.tempfiles = []
480
480
481 # Keep track of readline usage (later set by init_readline)
481 # Keep track of readline usage (later set by init_readline)
482 self.has_readline = False
482 self.has_readline = False
483
483
484 # keep track of where we started running (mainly for crash post-mortem)
484 # keep track of where we started running (mainly for crash post-mortem)
485 # This is not being used anywhere currently.
485 # This is not being used anywhere currently.
486 self.starting_dir = os.getcwd()
486 self.starting_dir = os.getcwd()
487
487
488 # Indentation management
488 # Indentation management
489 self.indent_current_nsp = 0
489 self.indent_current_nsp = 0
490
490
491 def init_term_title(self):
491 def init_term_title(self):
492 # Enable or disable the terminal title.
492 # Enable or disable the terminal title.
493 if self.term_title:
493 if self.term_title:
494 toggle_set_term_title(True)
494 toggle_set_term_title(True)
495 set_term_title('IPython: ' + abbrev_cwd())
495 set_term_title('IPython: ' + abbrev_cwd())
496 else:
496 else:
497 toggle_set_term_title(False)
497 toggle_set_term_title(False)
498
498
499 def init_usage(self, usage=None):
499 def init_usage(self, usage=None):
500 if usage is None:
500 if usage is None:
501 self.usage = interactive_usage
501 self.usage = interactive_usage
502 else:
502 else:
503 self.usage = usage
503 self.usage = usage
504
504
505 def init_encoding(self):
505 def init_encoding(self):
506 # Get system encoding at startup time. Certain terminals (like Emacs
506 # Get system encoding at startup time. Certain terminals (like Emacs
507 # under Win32 have it set to None, and we need to have a known valid
507 # under Win32 have it set to None, and we need to have a known valid
508 # encoding to use in the raw_input() method
508 # encoding to use in the raw_input() method
509 try:
509 try:
510 self.stdin_encoding = sys.stdin.encoding or 'ascii'
510 self.stdin_encoding = sys.stdin.encoding or 'ascii'
511 except AttributeError:
511 except AttributeError:
512 self.stdin_encoding = 'ascii'
512 self.stdin_encoding = 'ascii'
513
513
514 def init_syntax_highlighting(self):
514 def init_syntax_highlighting(self):
515 # Python source parser/formatter for syntax highlighting
515 # Python source parser/formatter for syntax highlighting
516 pyformat = PyColorize.Parser().format
516 pyformat = PyColorize.Parser().format
517 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
517 self.pycolorize = lambda src: pyformat(src,'str',self.colors)
518
518
519 def init_pushd_popd_magic(self):
519 def init_pushd_popd_magic(self):
520 # for pushd/popd management
520 # for pushd/popd management
521 try:
521 try:
522 self.home_dir = get_home_dir()
522 self.home_dir = get_home_dir()
523 except HomeDirError, msg:
523 except HomeDirError, msg:
524 fatal(msg)
524 fatal(msg)
525
525
526 self.dir_stack = []
526 self.dir_stack = []
527
527
528 def init_logger(self):
528 def init_logger(self):
529 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
529 self.logger = Logger(self, logfname='ipython_log.py', logmode='rotate')
530 # local shortcut, this is used a LOT
530 # local shortcut, this is used a LOT
531 self.log = self.logger.log
531 self.log = self.logger.log
532
532
533 def init_logstart(self):
533 def init_logstart(self):
534 if self.logappend:
534 if self.logappend:
535 self.magic_logstart(self.logappend + ' append')
535 self.magic_logstart(self.logappend + ' append')
536 elif self.logfile:
536 elif self.logfile:
537 self.magic_logstart(self.logfile)
537 self.magic_logstart(self.logfile)
538 elif self.logstart:
538 elif self.logstart:
539 self.magic_logstart()
539 self.magic_logstart()
540
540
541 def init_builtins(self):
541 def init_builtins(self):
542 self.builtin_trap = BuiltinTrap(self)
542 self.builtin_trap = BuiltinTrap(self)
543
543
544 def init_inspector(self):
544 def init_inspector(self):
545 # Object inspector
545 # Object inspector
546 self.inspector = oinspect.Inspector(oinspect.InspectColors,
546 self.inspector = oinspect.Inspector(oinspect.InspectColors,
547 PyColorize.ANSICodeColors,
547 PyColorize.ANSICodeColors,
548 'NoColor',
548 'NoColor',
549 self.object_info_string_level)
549 self.object_info_string_level)
550
550
551 def init_prompts(self):
551 def init_prompts(self):
552 # Initialize cache, set in/out prompts and printing system
552 # Initialize cache, set in/out prompts and printing system
553 self.outputcache = CachedOutput(self,
553 self.outputcache = CachedOutput(self,
554 self.cache_size,
554 self.cache_size,
555 self.pprint,
555 self.pprint,
556 input_sep = self.separate_in,
556 input_sep = self.separate_in,
557 output_sep = self.separate_out,
557 output_sep = self.separate_out,
558 output_sep2 = self.separate_out2,
558 output_sep2 = self.separate_out2,
559 ps1 = self.prompt_in1,
559 ps1 = self.prompt_in1,
560 ps2 = self.prompt_in2,
560 ps2 = self.prompt_in2,
561 ps_out = self.prompt_out,
561 ps_out = self.prompt_out,
562 pad_left = self.prompts_pad_left)
562 pad_left = self.prompts_pad_left)
563
563
564 # user may have over-ridden the default print hook:
564 # user may have over-ridden the default print hook:
565 try:
565 try:
566 self.outputcache.__class__.display = self.hooks.display
566 self.outputcache.__class__.display = self.hooks.display
567 except AttributeError:
567 except AttributeError:
568 pass
568 pass
569
569
570 def init_displayhook(self):
570 def init_displayhook(self):
571 self.display_trap = DisplayTrap(self, self.outputcache)
571 self.display_trap = DisplayTrap(self, self.outputcache)
572
572
573 def init_reload_doctest(self):
573 def init_reload_doctest(self):
574 # Do a proper resetting of doctest, including the necessary displayhook
574 # Do a proper resetting of doctest, including the necessary displayhook
575 # monkeypatching
575 # monkeypatching
576 try:
576 try:
577 doctest_reload()
577 doctest_reload()
578 except ImportError:
578 except ImportError:
579 warn("doctest module does not exist.")
579 warn("doctest module does not exist.")
580
580
581 #-------------------------------------------------------------------------
581 #-------------------------------------------------------------------------
582 # Things related to the banner
582 # Things related to the banner
583 #-------------------------------------------------------------------------
583 #-------------------------------------------------------------------------
584
584
585 def init_banner(self, banner1, banner2, display_banner):
585 def init_banner(self, banner1, banner2, display_banner):
586 if banner1 is not None:
586 if banner1 is not None:
587 self.banner1 = banner1
587 self.banner1 = banner1
588 if banner2 is not None:
588 if banner2 is not None:
589 self.banner2 = banner2
589 self.banner2 = banner2
590 if display_banner is not None:
590 if display_banner is not None:
591 self.display_banner = display_banner
591 self.display_banner = display_banner
592 self.compute_banner()
592 self.compute_banner()
593
593
594 def show_banner(self, banner=None):
594 def show_banner(self, banner=None):
595 if banner is None:
595 if banner is None:
596 banner = self.banner
596 banner = self.banner
597 self.write(banner)
597 self.write(banner)
598
598
599 def compute_banner(self):
599 def compute_banner(self):
600 self.banner = self.banner1 + '\n'
600 self.banner = self.banner1 + '\n'
601 if self.profile:
601 if self.profile:
602 self.banner += '\nIPython profile: %s\n' % self.profile
602 self.banner += '\nIPython profile: %s\n' % self.profile
603 if self.banner2:
603 if self.banner2:
604 self.banner += '\n' + self.banner2 + '\n'
604 self.banner += '\n' + self.banner2 + '\n'
605
605
606 #-------------------------------------------------------------------------
606 #-------------------------------------------------------------------------
607 # Things related to injections into the sys module
607 # Things related to injections into the sys module
608 #-------------------------------------------------------------------------
608 #-------------------------------------------------------------------------
609
609
610 def save_sys_module_state(self):
610 def save_sys_module_state(self):
611 """Save the state of hooks in the sys module.
611 """Save the state of hooks in the sys module.
612
612
613 This has to be called after self.user_ns is created.
613 This has to be called after self.user_ns is created.
614 """
614 """
615 self._orig_sys_module_state = {}
615 self._orig_sys_module_state = {}
616 self._orig_sys_module_state['stdin'] = sys.stdin
616 self._orig_sys_module_state['stdin'] = sys.stdin
617 self._orig_sys_module_state['stdout'] = sys.stdout
617 self._orig_sys_module_state['stdout'] = sys.stdout
618 self._orig_sys_module_state['stderr'] = sys.stderr
618 self._orig_sys_module_state['stderr'] = sys.stderr
619 self._orig_sys_module_state['excepthook'] = sys.excepthook
619 self._orig_sys_module_state['excepthook'] = sys.excepthook
620 try:
620 try:
621 self._orig_sys_modules_main_name = self.user_ns['__name__']
621 self._orig_sys_modules_main_name = self.user_ns['__name__']
622 except KeyError:
622 except KeyError:
623 pass
623 pass
624
624
625 def restore_sys_module_state(self):
625 def restore_sys_module_state(self):
626 """Restore the state of the sys module."""
626 """Restore the state of the sys module."""
627 try:
627 try:
628 for k, v in self._orig_sys_module_state.items():
628 for k, v in self._orig_sys_module_state.items():
629 setattr(sys, k, v)
629 setattr(sys, k, v)
630 except AttributeError:
630 except AttributeError:
631 pass
631 pass
632 try:
632 try:
633 delattr(sys, 'ipcompleter')
633 delattr(sys, 'ipcompleter')
634 except AttributeError:
634 except AttributeError:
635 pass
635 pass
636 # Reset what what done in self.init_sys_modules
636 # Reset what what done in self.init_sys_modules
637 try:
637 try:
638 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
638 sys.modules[self.user_ns['__name__']] = self._orig_sys_modules_main_name
639 except (AttributeError, KeyError):
639 except (AttributeError, KeyError):
640 pass
640 pass
641
641
642 #-------------------------------------------------------------------------
642 #-------------------------------------------------------------------------
643 # Things related to hooks
643 # Things related to hooks
644 #-------------------------------------------------------------------------
644 #-------------------------------------------------------------------------
645
645
646 def init_hooks(self):
646 def init_hooks(self):
647 # hooks holds pointers used for user-side customizations
647 # hooks holds pointers used for user-side customizations
648 self.hooks = Struct()
648 self.hooks = Struct()
649
649
650 self.strdispatchers = {}
650 self.strdispatchers = {}
651
651
652 # Set all default hooks, defined in the IPython.hooks module.
652 # Set all default hooks, defined in the IPython.hooks module.
653 import IPython.core.hooks
653 import IPython.core.hooks
654 hooks = IPython.core.hooks
654 hooks = IPython.core.hooks
655 for hook_name in hooks.__all__:
655 for hook_name in hooks.__all__:
656 # default hooks have priority 100, i.e. low; user hooks should have
656 # default hooks have priority 100, i.e. low; user hooks should have
657 # 0-100 priority
657 # 0-100 priority
658 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
658 self.set_hook(hook_name,getattr(hooks,hook_name), 100)
659
659
660 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
660 def set_hook(self,name,hook, priority = 50, str_key = None, re_key = None):
661 """set_hook(name,hook) -> sets an internal IPython hook.
661 """set_hook(name,hook) -> sets an internal IPython hook.
662
662
663 IPython exposes some of its internal API as user-modifiable hooks. By
663 IPython exposes some of its internal API as user-modifiable hooks. By
664 adding your function to one of these hooks, you can modify IPython's
664 adding your function to one of these hooks, you can modify IPython's
665 behavior to call at runtime your own routines."""
665 behavior to call at runtime your own routines."""
666
666
667 # At some point in the future, this should validate the hook before it
667 # At some point in the future, this should validate the hook before it
668 # accepts it. Probably at least check that the hook takes the number
668 # accepts it. Probably at least check that the hook takes the number
669 # of args it's supposed to.
669 # of args it's supposed to.
670
670
671 f = new.instancemethod(hook,self,self.__class__)
671 f = new.instancemethod(hook,self,self.__class__)
672
672
673 # check if the hook is for strdispatcher first
673 # check if the hook is for strdispatcher first
674 if str_key is not None:
674 if str_key is not None:
675 sdp = self.strdispatchers.get(name, StrDispatch())
675 sdp = self.strdispatchers.get(name, StrDispatch())
676 sdp.add_s(str_key, f, priority )
676 sdp.add_s(str_key, f, priority )
677 self.strdispatchers[name] = sdp
677 self.strdispatchers[name] = sdp
678 return
678 return
679 if re_key is not None:
679 if re_key is not None:
680 sdp = self.strdispatchers.get(name, StrDispatch())
680 sdp = self.strdispatchers.get(name, StrDispatch())
681 sdp.add_re(re.compile(re_key), f, priority )
681 sdp.add_re(re.compile(re_key), f, priority )
682 self.strdispatchers[name] = sdp
682 self.strdispatchers[name] = sdp
683 return
683 return
684
684
685 dp = getattr(self.hooks, name, None)
685 dp = getattr(self.hooks, name, None)
686 if name not in IPython.core.hooks.__all__:
686 if name not in IPython.core.hooks.__all__:
687 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
687 print "Warning! Hook '%s' is not one of %s" % (name, IPython.core.hooks.__all__ )
688 if not dp:
688 if not dp:
689 dp = IPython.core.hooks.CommandChainDispatcher()
689 dp = IPython.core.hooks.CommandChainDispatcher()
690
690
691 try:
691 try:
692 dp.add(f,priority)
692 dp.add(f,priority)
693 except AttributeError:
693 except AttributeError:
694 # it was not commandchain, plain old func - replace
694 # it was not commandchain, plain old func - replace
695 dp = f
695 dp = f
696
696
697 setattr(self.hooks,name, dp)
697 setattr(self.hooks,name, dp)
698
698
699 #-------------------------------------------------------------------------
699 #-------------------------------------------------------------------------
700 # Things related to the "main" module
700 # Things related to the "main" module
701 #-------------------------------------------------------------------------
701 #-------------------------------------------------------------------------
702
702
703 def new_main_mod(self,ns=None):
703 def new_main_mod(self,ns=None):
704 """Return a new 'main' module object for user code execution.
704 """Return a new 'main' module object for user code execution.
705 """
705 """
706 main_mod = self._user_main_module
706 main_mod = self._user_main_module
707 init_fakemod_dict(main_mod,ns)
707 init_fakemod_dict(main_mod,ns)
708 return main_mod
708 return main_mod
709
709
710 def cache_main_mod(self,ns,fname):
710 def cache_main_mod(self,ns,fname):
711 """Cache a main module's namespace.
711 """Cache a main module's namespace.
712
712
713 When scripts are executed via %run, we must keep a reference to the
713 When scripts are executed via %run, we must keep a reference to the
714 namespace of their __main__ module (a FakeModule instance) around so
714 namespace of their __main__ module (a FakeModule instance) around so
715 that Python doesn't clear it, rendering objects defined therein
715 that Python doesn't clear it, rendering objects defined therein
716 useless.
716 useless.
717
717
718 This method keeps said reference in a private dict, keyed by the
718 This method keeps said reference in a private dict, keyed by the
719 absolute path of the module object (which corresponds to the script
719 absolute path of the module object (which corresponds to the script
720 path). This way, for multiple executions of the same script we only
720 path). This way, for multiple executions of the same script we only
721 keep one copy of the namespace (the last one), thus preventing memory
721 keep one copy of the namespace (the last one), thus preventing memory
722 leaks from old references while allowing the objects from the last
722 leaks from old references while allowing the objects from the last
723 execution to be accessible.
723 execution to be accessible.
724
724
725 Note: we can not allow the actual FakeModule instances to be deleted,
725 Note: we can not allow the actual FakeModule instances to be deleted,
726 because of how Python tears down modules (it hard-sets all their
726 because of how Python tears down modules (it hard-sets all their
727 references to None without regard for reference counts). This method
727 references to None without regard for reference counts). This method
728 must therefore make a *copy* of the given namespace, to allow the
728 must therefore make a *copy* of the given namespace, to allow the
729 original module's __dict__ to be cleared and reused.
729 original module's __dict__ to be cleared and reused.
730
730
731
731
732 Parameters
732 Parameters
733 ----------
733 ----------
734 ns : a namespace (a dict, typically)
734 ns : a namespace (a dict, typically)
735
735
736 fname : str
736 fname : str
737 Filename associated with the namespace.
737 Filename associated with the namespace.
738
738
739 Examples
739 Examples
740 --------
740 --------
741
741
742 In [10]: import IPython
742 In [10]: import IPython
743
743
744 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
744 In [11]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
745
745
746 In [12]: IPython.__file__ in _ip._main_ns_cache
746 In [12]: IPython.__file__ in _ip._main_ns_cache
747 Out[12]: True
747 Out[12]: True
748 """
748 """
749 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
749 self._main_ns_cache[os.path.abspath(fname)] = ns.copy()
750
750
751 def clear_main_mod_cache(self):
751 def clear_main_mod_cache(self):
752 """Clear the cache of main modules.
752 """Clear the cache of main modules.
753
753
754 Mainly for use by utilities like %reset.
754 Mainly for use by utilities like %reset.
755
755
756 Examples
756 Examples
757 --------
757 --------
758
758
759 In [15]: import IPython
759 In [15]: import IPython
760
760
761 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
761 In [16]: _ip.cache_main_mod(IPython.__dict__,IPython.__file__)
762
762
763 In [17]: len(_ip._main_ns_cache) > 0
763 In [17]: len(_ip._main_ns_cache) > 0
764 Out[17]: True
764 Out[17]: True
765
765
766 In [18]: _ip.clear_main_mod_cache()
766 In [18]: _ip.clear_main_mod_cache()
767
767
768 In [19]: len(_ip._main_ns_cache) == 0
768 In [19]: len(_ip._main_ns_cache) == 0
769 Out[19]: True
769 Out[19]: True
770 """
770 """
771 self._main_ns_cache.clear()
771 self._main_ns_cache.clear()
772
772
773 #-------------------------------------------------------------------------
773 #-------------------------------------------------------------------------
774 # Things related to debugging
774 # Things related to debugging
775 #-------------------------------------------------------------------------
775 #-------------------------------------------------------------------------
776
776
777 def init_pdb(self):
777 def init_pdb(self):
778 # Set calling of pdb on exceptions
778 # Set calling of pdb on exceptions
779 # self.call_pdb is a property
779 # self.call_pdb is a property
780 self.call_pdb = self.pdb
780 self.call_pdb = self.pdb
781
781
782 def _get_call_pdb(self):
782 def _get_call_pdb(self):
783 return self._call_pdb
783 return self._call_pdb
784
784
785 def _set_call_pdb(self,val):
785 def _set_call_pdb(self,val):
786
786
787 if val not in (0,1,False,True):
787 if val not in (0,1,False,True):
788 raise ValueError,'new call_pdb value must be boolean'
788 raise ValueError,'new call_pdb value must be boolean'
789
789
790 # store value in instance
790 # store value in instance
791 self._call_pdb = val
791 self._call_pdb = val
792
792
793 # notify the actual exception handlers
793 # notify the actual exception handlers
794 self.InteractiveTB.call_pdb = val
794 self.InteractiveTB.call_pdb = val
795 if self.isthreaded:
795 if self.isthreaded:
796 try:
796 try:
797 self.sys_excepthook.call_pdb = val
797 self.sys_excepthook.call_pdb = val
798 except:
798 except:
799 warn('Failed to activate pdb for threaded exception handler')
799 warn('Failed to activate pdb for threaded exception handler')
800
800
801 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
801 call_pdb = property(_get_call_pdb,_set_call_pdb,None,
802 'Control auto-activation of pdb at exceptions')
802 'Control auto-activation of pdb at exceptions')
803
803
804 def debugger(self,force=False):
804 def debugger(self,force=False):
805 """Call the pydb/pdb debugger.
805 """Call the pydb/pdb debugger.
806
806
807 Keywords:
807 Keywords:
808
808
809 - force(False): by default, this routine checks the instance call_pdb
809 - force(False): by default, this routine checks the instance call_pdb
810 flag and does not actually invoke the debugger if the flag is false.
810 flag and does not actually invoke the debugger if the flag is false.
811 The 'force' option forces the debugger to activate even if the flag
811 The 'force' option forces the debugger to activate even if the flag
812 is false.
812 is false.
813 """
813 """
814
814
815 if not (force or self.call_pdb):
815 if not (force or self.call_pdb):
816 return
816 return
817
817
818 if not hasattr(sys,'last_traceback'):
818 if not hasattr(sys,'last_traceback'):
819 error('No traceback has been produced, nothing to debug.')
819 error('No traceback has been produced, nothing to debug.')
820 return
820 return
821
821
822 # use pydb if available
822 # use pydb if available
823 if debugger.has_pydb:
823 if debugger.has_pydb:
824 from pydb import pm
824 from pydb import pm
825 else:
825 else:
826 # fallback to our internal debugger
826 # fallback to our internal debugger
827 pm = lambda : self.InteractiveTB.debugger(force=True)
827 pm = lambda : self.InteractiveTB.debugger(force=True)
828 self.history_saving_wrapper(pm)()
828 self.history_saving_wrapper(pm)()
829
829
830 #-------------------------------------------------------------------------
830 #-------------------------------------------------------------------------
831 # Things related to IPython's various namespaces
831 # Things related to IPython's various namespaces
832 #-------------------------------------------------------------------------
832 #-------------------------------------------------------------------------
833
833
834 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
834 def init_create_namespaces(self, user_ns=None, user_global_ns=None):
835 # Create the namespace where the user will operate. user_ns is
835 # Create the namespace where the user will operate. user_ns is
836 # normally the only one used, and it is passed to the exec calls as
836 # normally the only one used, and it is passed to the exec calls as
837 # the locals argument. But we do carry a user_global_ns namespace
837 # the locals argument. But we do carry a user_global_ns namespace
838 # given as the exec 'globals' argument, This is useful in embedding
838 # given as the exec 'globals' argument, This is useful in embedding
839 # situations where the ipython shell opens in a context where the
839 # situations where the ipython shell opens in a context where the
840 # distinction between locals and globals is meaningful. For
840 # distinction between locals and globals is meaningful. For
841 # non-embedded contexts, it is just the same object as the user_ns dict.
841 # non-embedded contexts, it is just the same object as the user_ns dict.
842
842
843 # FIXME. For some strange reason, __builtins__ is showing up at user
843 # FIXME. For some strange reason, __builtins__ is showing up at user
844 # level as a dict instead of a module. This is a manual fix, but I
844 # level as a dict instead of a module. This is a manual fix, but I
845 # should really track down where the problem is coming from. Alex
845 # should really track down where the problem is coming from. Alex
846 # Schmolck reported this problem first.
846 # Schmolck reported this problem first.
847
847
848 # A useful post by Alex Martelli on this topic:
848 # A useful post by Alex Martelli on this topic:
849 # Re: inconsistent value from __builtins__
849 # Re: inconsistent value from __builtins__
850 # Von: Alex Martelli <aleaxit@yahoo.com>
850 # Von: Alex Martelli <aleaxit@yahoo.com>
851 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
851 # Datum: Freitag 01 Oktober 2004 04:45:34 nachmittags/abends
852 # Gruppen: comp.lang.python
852 # Gruppen: comp.lang.python
853
853
854 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
854 # Michael Hohn <hohn@hooknose.lbl.gov> wrote:
855 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
855 # > >>> print type(builtin_check.get_global_binding('__builtins__'))
856 # > <type 'dict'>
856 # > <type 'dict'>
857 # > >>> print type(__builtins__)
857 # > >>> print type(__builtins__)
858 # > <type 'module'>
858 # > <type 'module'>
859 # > Is this difference in return value intentional?
859 # > Is this difference in return value intentional?
860
860
861 # Well, it's documented that '__builtins__' can be either a dictionary
861 # Well, it's documented that '__builtins__' can be either a dictionary
862 # or a module, and it's been that way for a long time. Whether it's
862 # or a module, and it's been that way for a long time. Whether it's
863 # intentional (or sensible), I don't know. In any case, the idea is
863 # intentional (or sensible), I don't know. In any case, the idea is
864 # that if you need to access the built-in namespace directly, you
864 # that if you need to access the built-in namespace directly, you
865 # should start with "import __builtin__" (note, no 's') which will
865 # should start with "import __builtin__" (note, no 's') which will
866 # definitely give you a module. Yeah, it's somewhat confusing:-(.
866 # definitely give you a module. Yeah, it's somewhat confusing:-(.
867
867
868 # These routines return properly built dicts as needed by the rest of
868 # These routines return properly built dicts as needed by the rest of
869 # the code, and can also be used by extension writers to generate
869 # the code, and can also be used by extension writers to generate
870 # properly initialized namespaces.
870 # properly initialized namespaces.
871 user_ns, user_global_ns = make_user_namespaces(user_ns, user_global_ns)
871 user_ns, user_global_ns = make_user_namespaces(user_ns, user_global_ns)
872
872
873 # Assign namespaces
873 # Assign namespaces
874 # This is the namespace where all normal user variables live
874 # This is the namespace where all normal user variables live
875 self.user_ns = user_ns
875 self.user_ns = user_ns
876 self.user_global_ns = user_global_ns
876 self.user_global_ns = user_global_ns
877
877
878 # An auxiliary namespace that checks what parts of the user_ns were
878 # An auxiliary namespace that checks what parts of the user_ns were
879 # loaded at startup, so we can list later only variables defined in
879 # loaded at startup, so we can list later only variables defined in
880 # actual interactive use. Since it is always a subset of user_ns, it
880 # actual interactive use. Since it is always a subset of user_ns, it
881 # doesn't need to be separately tracked in the ns_table.
881 # doesn't need to be separately tracked in the ns_table.
882 self.user_config_ns = {}
882 self.user_config_ns = {}
883
883
884 # A namespace to keep track of internal data structures to prevent
884 # A namespace to keep track of internal data structures to prevent
885 # them from cluttering user-visible stuff. Will be updated later
885 # them from cluttering user-visible stuff. Will be updated later
886 self.internal_ns = {}
886 self.internal_ns = {}
887
887
888 # Now that FakeModule produces a real module, we've run into a nasty
888 # Now that FakeModule produces a real module, we've run into a nasty
889 # problem: after script execution (via %run), the module where the user
889 # problem: after script execution (via %run), the module where the user
890 # code ran is deleted. Now that this object is a true module (needed
890 # code ran is deleted. Now that this object is a true module (needed
891 # so docetst and other tools work correctly), the Python module
891 # so docetst and other tools work correctly), the Python module
892 # teardown mechanism runs over it, and sets to None every variable
892 # teardown mechanism runs over it, and sets to None every variable
893 # present in that module. Top-level references to objects from the
893 # present in that module. Top-level references to objects from the
894 # script survive, because the user_ns is updated with them. However,
894 # script survive, because the user_ns is updated with them. However,
895 # calling functions defined in the script that use other things from
895 # calling functions defined in the script that use other things from
896 # the script will fail, because the function's closure had references
896 # the script will fail, because the function's closure had references
897 # to the original objects, which are now all None. So we must protect
897 # to the original objects, which are now all None. So we must protect
898 # these modules from deletion by keeping a cache.
898 # these modules from deletion by keeping a cache.
899 #
899 #
900 # To avoid keeping stale modules around (we only need the one from the
900 # To avoid keeping stale modules around (we only need the one from the
901 # last run), we use a dict keyed with the full path to the script, so
901 # last run), we use a dict keyed with the full path to the script, so
902 # only the last version of the module is held in the cache. Note,
902 # only the last version of the module is held in the cache. Note,
903 # however, that we must cache the module *namespace contents* (their
903 # however, that we must cache the module *namespace contents* (their
904 # __dict__). Because if we try to cache the actual modules, old ones
904 # __dict__). Because if we try to cache the actual modules, old ones
905 # (uncached) could be destroyed while still holding references (such as
905 # (uncached) could be destroyed while still holding references (such as
906 # those held by GUI objects that tend to be long-lived)>
906 # those held by GUI objects that tend to be long-lived)>
907 #
907 #
908 # The %reset command will flush this cache. See the cache_main_mod()
908 # The %reset command will flush this cache. See the cache_main_mod()
909 # and clear_main_mod_cache() methods for details on use.
909 # and clear_main_mod_cache() methods for details on use.
910
910
911 # This is the cache used for 'main' namespaces
911 # This is the cache used for 'main' namespaces
912 self._main_ns_cache = {}
912 self._main_ns_cache = {}
913 # And this is the single instance of FakeModule whose __dict__ we keep
913 # And this is the single instance of FakeModule whose __dict__ we keep
914 # copying and clearing for reuse on each %run
914 # copying and clearing for reuse on each %run
915 self._user_main_module = FakeModule()
915 self._user_main_module = FakeModule()
916
916
917 # A table holding all the namespaces IPython deals with, so that
917 # A table holding all the namespaces IPython deals with, so that
918 # introspection facilities can search easily.
918 # introspection facilities can search easily.
919 self.ns_table = {'user':user_ns,
919 self.ns_table = {'user':user_ns,
920 'user_global':user_global_ns,
920 'user_global':user_global_ns,
921 'internal':self.internal_ns,
921 'internal':self.internal_ns,
922 'builtin':__builtin__.__dict__
922 'builtin':__builtin__.__dict__
923 }
923 }
924
924
925 # Similarly, track all namespaces where references can be held and that
925 # Similarly, track all namespaces where references can be held and that
926 # we can safely clear (so it can NOT include builtin). This one can be
926 # we can safely clear (so it can NOT include builtin). This one can be
927 # a simple list.
927 # a simple list.
928 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
928 self.ns_refs_table = [ user_ns, user_global_ns, self.user_config_ns,
929 self.internal_ns, self._main_ns_cache ]
929 self.internal_ns, self._main_ns_cache ]
930
930
931 def init_sys_modules(self):
931 def init_sys_modules(self):
932 # We need to insert into sys.modules something that looks like a
932 # We need to insert into sys.modules something that looks like a
933 # module but which accesses the IPython namespace, for shelve and
933 # module but which accesses the IPython namespace, for shelve and
934 # pickle to work interactively. Normally they rely on getting
934 # pickle to work interactively. Normally they rely on getting
935 # everything out of __main__, but for embedding purposes each IPython
935 # everything out of __main__, but for embedding purposes each IPython
936 # instance has its own private namespace, so we can't go shoving
936 # instance has its own private namespace, so we can't go shoving
937 # everything into __main__.
937 # everything into __main__.
938
938
939 # note, however, that we should only do this for non-embedded
939 # note, however, that we should only do this for non-embedded
940 # ipythons, which really mimic the __main__.__dict__ with their own
940 # ipythons, which really mimic the __main__.__dict__ with their own
941 # namespace. Embedded instances, on the other hand, should not do
941 # namespace. Embedded instances, on the other hand, should not do
942 # this because they need to manage the user local/global namespaces
942 # this because they need to manage the user local/global namespaces
943 # only, but they live within a 'normal' __main__ (meaning, they
943 # only, but they live within a 'normal' __main__ (meaning, they
944 # shouldn't overtake the execution environment of the script they're
944 # shouldn't overtake the execution environment of the script they're
945 # embedded in).
945 # embedded in).
946
946
947 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
947 # This is overridden in the InteractiveShellEmbed subclass to a no-op.
948
948
949 try:
949 try:
950 main_name = self.user_ns['__name__']
950 main_name = self.user_ns['__name__']
951 except KeyError:
951 except KeyError:
952 raise KeyError('user_ns dictionary MUST have a "__name__" key')
952 raise KeyError('user_ns dictionary MUST have a "__name__" key')
953 else:
953 else:
954 sys.modules[main_name] = FakeModule(self.user_ns)
954 sys.modules[main_name] = FakeModule(self.user_ns)
955
955
956 def init_user_ns(self):
956 def init_user_ns(self):
957 """Initialize all user-visible namespaces to their minimum defaults.
957 """Initialize all user-visible namespaces to their minimum defaults.
958
958
959 Certain history lists are also initialized here, as they effectively
959 Certain history lists are also initialized here, as they effectively
960 act as user namespaces.
960 act as user namespaces.
961
961
962 Notes
962 Notes
963 -----
963 -----
964 All data structures here are only filled in, they are NOT reset by this
964 All data structures here are only filled in, they are NOT reset by this
965 method. If they were not empty before, data will simply be added to
965 method. If they were not empty before, data will simply be added to
966 therm.
966 therm.
967 """
967 """
968 # This function works in two parts: first we put a few things in
968 # This function works in two parts: first we put a few things in
969 # user_ns, and we sync that contents into user_config_ns so that these
969 # user_ns, and we sync that contents into user_config_ns so that these
970 # initial variables aren't shown by %who. After the sync, we add the
970 # initial variables aren't shown by %who. After the sync, we add the
971 # rest of what we *do* want the user to see with %who even on a new
971 # rest of what we *do* want the user to see with %who even on a new
972 # session.
972 # session.
973 ns = {}
973 ns = {}
974
974
975 # Put 'help' in the user namespace
975 # Put 'help' in the user namespace
976 try:
976 try:
977 from site import _Helper
977 from site import _Helper
978 ns['help'] = _Helper()
978 ns['help'] = _Helper()
979 except ImportError:
979 except ImportError:
980 warn('help() not available - check site.py')
980 warn('help() not available - check site.py')
981
981
982 # make global variables for user access to the histories
982 # make global variables for user access to the histories
983 ns['_ih'] = self.input_hist
983 ns['_ih'] = self.input_hist
984 ns['_oh'] = self.output_hist
984 ns['_oh'] = self.output_hist
985 ns['_dh'] = self.dir_hist
985 ns['_dh'] = self.dir_hist
986
986
987 ns['_sh'] = shadowns
987 ns['_sh'] = shadowns
988
988
989 # Sync what we've added so far to user_config_ns so these aren't seen
989 # Sync what we've added so far to user_config_ns so these aren't seen
990 # by %who
990 # by %who
991 self.user_config_ns.update(ns)
991 self.user_config_ns.update(ns)
992
992
993 # Now, continue adding more contents
993 # Now, continue adding more contents
994
994
995 # user aliases to input and output histories
995 # user aliases to input and output histories
996 ns['In'] = self.input_hist
996 ns['In'] = self.input_hist
997 ns['Out'] = self.output_hist
997 ns['Out'] = self.output_hist
998
998
999 # Store myself as the public api!!!
999 # Store myself as the public api!!!
1000 ns['get_ipython'] = self.get_ipython
1000 ns['get_ipython'] = self.get_ipython
1001
1001
1002 # And update the real user's namespace
1002 # And update the real user's namespace
1003 self.user_ns.update(ns)
1003 self.user_ns.update(ns)
1004
1004
1005
1005
1006 def reset(self):
1006 def reset(self):
1007 """Clear all internal namespaces.
1007 """Clear all internal namespaces.
1008
1008
1009 Note that this is much more aggressive than %reset, since it clears
1009 Note that this is much more aggressive than %reset, since it clears
1010 fully all namespaces, as well as all input/output lists.
1010 fully all namespaces, as well as all input/output lists.
1011 """
1011 """
1012 for ns in self.ns_refs_table:
1012 for ns in self.ns_refs_table:
1013 ns.clear()
1013 ns.clear()
1014
1014
1015 self.alias_manager.clear_aliases()
1015 self.alias_manager.clear_aliases()
1016
1016
1017 # Clear input and output histories
1017 # Clear input and output histories
1018 self.input_hist[:] = []
1018 self.input_hist[:] = []
1019 self.input_hist_raw[:] = []
1019 self.input_hist_raw[:] = []
1020 self.output_hist.clear()
1020 self.output_hist.clear()
1021
1021
1022 # Restore the user namespaces to minimal usability
1022 # Restore the user namespaces to minimal usability
1023 self.init_user_ns()
1023 self.init_user_ns()
1024
1024
1025 # Restore the default and user aliases
1025 # Restore the default and user aliases
1026 self.alias_manager.init_aliases()
1026 self.alias_manager.init_aliases()
1027
1027
1028 def push(self, variables, interactive=True):
1028 def push(self, variables, interactive=True):
1029 """Inject a group of variables into the IPython user namespace.
1029 """Inject a group of variables into the IPython user namespace.
1030
1030
1031 Parameters
1031 Parameters
1032 ----------
1032 ----------
1033 variables : dict, str or list/tuple of str
1033 variables : dict, str or list/tuple of str
1034 The variables to inject into the user's namespace. If a dict,
1034 The variables to inject into the user's namespace. If a dict,
1035 a simple update is done. If a str, the string is assumed to
1035 a simple update is done. If a str, the string is assumed to
1036 have variable names separated by spaces. A list/tuple of str
1036 have variable names separated by spaces. A list/tuple of str
1037 can also be used to give the variable names. If just the variable
1037 can also be used to give the variable names. If just the variable
1038 names are give (list/tuple/str) then the variable values looked
1038 names are give (list/tuple/str) then the variable values looked
1039 up in the callers frame.
1039 up in the callers frame.
1040 interactive : bool
1040 interactive : bool
1041 If True (default), the variables will be listed with the ``who``
1041 If True (default), the variables will be listed with the ``who``
1042 magic.
1042 magic.
1043 """
1043 """
1044 vdict = None
1044 vdict = None
1045
1045
1046 # We need a dict of name/value pairs to do namespace updates.
1046 # We need a dict of name/value pairs to do namespace updates.
1047 if isinstance(variables, dict):
1047 if isinstance(variables, dict):
1048 vdict = variables
1048 vdict = variables
1049 elif isinstance(variables, (basestring, list, tuple)):
1049 elif isinstance(variables, (basestring, list, tuple)):
1050 if isinstance(variables, basestring):
1050 if isinstance(variables, basestring):
1051 vlist = variables.split()
1051 vlist = variables.split()
1052 else:
1052 else:
1053 vlist = variables
1053 vlist = variables
1054 vdict = {}
1054 vdict = {}
1055 cf = sys._getframe(1)
1055 cf = sys._getframe(1)
1056 for name in vlist:
1056 for name in vlist:
1057 try:
1057 try:
1058 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1058 vdict[name] = eval(name, cf.f_globals, cf.f_locals)
1059 except:
1059 except:
1060 print ('Could not get variable %s from %s' %
1060 print ('Could not get variable %s from %s' %
1061 (name,cf.f_code.co_name))
1061 (name,cf.f_code.co_name))
1062 else:
1062 else:
1063 raise ValueError('variables must be a dict/str/list/tuple')
1063 raise ValueError('variables must be a dict/str/list/tuple')
1064
1064
1065 # Propagate variables to user namespace
1065 # Propagate variables to user namespace
1066 self.user_ns.update(vdict)
1066 self.user_ns.update(vdict)
1067
1067
1068 # And configure interactive visibility
1068 # And configure interactive visibility
1069 config_ns = self.user_config_ns
1069 config_ns = self.user_config_ns
1070 if interactive:
1070 if interactive:
1071 for name, val in vdict.iteritems():
1071 for name, val in vdict.iteritems():
1072 config_ns.pop(name, None)
1072 config_ns.pop(name, None)
1073 else:
1073 else:
1074 for name,val in vdict.iteritems():
1074 for name,val in vdict.iteritems():
1075 config_ns[name] = val
1075 config_ns[name] = val
1076
1076
1077 #-------------------------------------------------------------------------
1077 #-------------------------------------------------------------------------
1078 # Things related to history management
1078 # Things related to history management
1079 #-------------------------------------------------------------------------
1079 #-------------------------------------------------------------------------
1080
1080
1081 def init_history(self):
1081 def init_history(self):
1082 # List of input with multi-line handling.
1082 # List of input with multi-line handling.
1083 self.input_hist = InputList()
1083 self.input_hist = InputList()
1084 # This one will hold the 'raw' input history, without any
1084 # This one will hold the 'raw' input history, without any
1085 # pre-processing. This will allow users to retrieve the input just as
1085 # pre-processing. This will allow users to retrieve the input just as
1086 # it was exactly typed in by the user, with %hist -r.
1086 # it was exactly typed in by the user, with %hist -r.
1087 self.input_hist_raw = InputList()
1087 self.input_hist_raw = InputList()
1088
1088
1089 # list of visited directories
1089 # list of visited directories
1090 try:
1090 try:
1091 self.dir_hist = [os.getcwd()]
1091 self.dir_hist = [os.getcwd()]
1092 except OSError:
1092 except OSError:
1093 self.dir_hist = []
1093 self.dir_hist = []
1094
1094
1095 # dict of output history
1095 # dict of output history
1096 self.output_hist = {}
1096 self.output_hist = {}
1097
1097
1098 # Now the history file
1098 # Now the history file
1099 if self.profile:
1099 if self.profile:
1100 histfname = 'history-%s' % self.profile
1100 histfname = 'history-%s' % self.profile
1101 else:
1101 else:
1102 histfname = 'history'
1102 histfname = 'history'
1103 self.histfile = os.path.join(self.ipython_dir, histfname)
1103 self.histfile = os.path.join(self.ipython_dir, histfname)
1104
1104
1105 # Fill the history zero entry, user counter starts at 1
1105 # Fill the history zero entry, user counter starts at 1
1106 self.input_hist.append('\n')
1106 self.input_hist.append('\n')
1107 self.input_hist_raw.append('\n')
1107 self.input_hist_raw.append('\n')
1108
1108
1109 def init_shadow_hist(self):
1109 def init_shadow_hist(self):
1110 try:
1110 try:
1111 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1111 self.db = pickleshare.PickleShareDB(self.ipython_dir + "/db")
1112 except exceptions.UnicodeDecodeError:
1112 except exceptions.UnicodeDecodeError:
1113 print "Your ipython_dir can't be decoded to unicode!"
1113 print "Your ipython_dir can't be decoded to unicode!"
1114 print "Please set HOME environment variable to something that"
1114 print "Please set HOME environment variable to something that"
1115 print r"only has ASCII characters, e.g. c:\home"
1115 print r"only has ASCII characters, e.g. c:\home"
1116 print "Now it is", self.ipython_dir
1116 print "Now it is", self.ipython_dir
1117 sys.exit()
1117 sys.exit()
1118 self.shadowhist = ipcorehist.ShadowHist(self.db)
1118 self.shadowhist = ipcorehist.ShadowHist(self.db)
1119
1119
1120 def savehist(self):
1120 def savehist(self):
1121 """Save input history to a file (via readline library)."""
1121 """Save input history to a file (via readline library)."""
1122
1122
1123 try:
1123 try:
1124 self.readline.write_history_file(self.histfile)
1124 self.readline.write_history_file(self.histfile)
1125 except:
1125 except:
1126 print 'Unable to save IPython command history to file: ' + \
1126 print 'Unable to save IPython command history to file: ' + \
1127 `self.histfile`
1127 `self.histfile`
1128
1128
1129 def reloadhist(self):
1129 def reloadhist(self):
1130 """Reload the input history from disk file."""
1130 """Reload the input history from disk file."""
1131
1131
1132 try:
1132 try:
1133 self.readline.clear_history()
1133 self.readline.clear_history()
1134 self.readline.read_history_file(self.shell.histfile)
1134 self.readline.read_history_file(self.shell.histfile)
1135 except AttributeError:
1135 except AttributeError:
1136 pass
1136 pass
1137
1137
1138 def history_saving_wrapper(self, func):
1138 def history_saving_wrapper(self, func):
1139 """ Wrap func for readline history saving
1139 """ Wrap func for readline history saving
1140
1140
1141 Convert func into callable that saves & restores
1141 Convert func into callable that saves & restores
1142 history around the call """
1142 history around the call """
1143
1143
1144 if not self.has_readline:
1144 if not self.has_readline:
1145 return func
1145 return func
1146
1146
1147 def wrapper():
1147 def wrapper():
1148 self.savehist()
1148 self.savehist()
1149 try:
1149 try:
1150 func()
1150 func()
1151 finally:
1151 finally:
1152 readline.read_history_file(self.histfile)
1152 readline.read_history_file(self.histfile)
1153 return wrapper
1153 return wrapper
1154
1154
1155 #-------------------------------------------------------------------------
1155 #-------------------------------------------------------------------------
1156 # Things related to exception handling and tracebacks (not debugging)
1156 # Things related to exception handling and tracebacks (not debugging)
1157 #-------------------------------------------------------------------------
1157 #-------------------------------------------------------------------------
1158
1158
1159 def init_traceback_handlers(self, custom_exceptions):
1159 def init_traceback_handlers(self, custom_exceptions):
1160 # Syntax error handler.
1160 # Syntax error handler.
1161 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1161 self.SyntaxTB = SyntaxTB(color_scheme='NoColor')
1162
1162
1163 # The interactive one is initialized with an offset, meaning we always
1163 # The interactive one is initialized with an offset, meaning we always
1164 # want to remove the topmost item in the traceback, which is our own
1164 # want to remove the topmost item in the traceback, which is our own
1165 # internal code. Valid modes: ['Plain','Context','Verbose']
1165 # internal code. Valid modes: ['Plain','Context','Verbose']
1166 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1166 self.InteractiveTB = ultratb.AutoFormattedTB(mode = 'Plain',
1167 color_scheme='NoColor',
1167 color_scheme='NoColor',
1168 tb_offset = 1)
1168 tb_offset = 1)
1169
1169
1170 # IPython itself shouldn't crash. This will produce a detailed
1170 # The instance will store a pointer to the system-wide exception hook,
1171 # post-mortem if it does. But we only install the crash handler for
1171 # so that runtime code (such as magics) can access it. This is because
1172 # non-threaded shells, the threaded ones use a normal verbose reporter
1172 # during the read-eval loop, it may get temporarily overwritten.
1173 # and lose the crash handler. This is because exceptions in the main
1173 self.sys_excepthook = sys.excepthook
1174 # thread (such as in GUI code) propagate directly to sys.excepthook,
1175 # and there's no point in printing crash dumps for every user exception.
1176 if self.isthreaded:
1177 ipCrashHandler = ultratb.FormattedTB()
1178 else:
1179 from IPython.core import crashhandler
1180 ipCrashHandler = crashhandler.IPythonCrashHandler(self)
1181 self.set_crash_handler(ipCrashHandler)
1182
1174
1183 # and add any custom exception handlers the user may have specified
1175 # and add any custom exception handlers the user may have specified
1184 self.set_custom_exc(*custom_exceptions)
1176 self.set_custom_exc(*custom_exceptions)
1185
1177
1186 def set_crash_handler(self, crashHandler):
1187 """Set the IPython crash handler.
1188
1189 This must be a callable with a signature suitable for use as
1190 sys.excepthook."""
1191
1192 # Install the given crash handler as the Python exception hook
1193 sys.excepthook = crashHandler
1194
1195 # The instance will store a pointer to this, so that runtime code
1196 # (such as magics) can access it. This is because during the
1197 # read-eval loop, it gets temporarily overwritten (to deal with GUI
1198 # frameworks).
1199 self.sys_excepthook = sys.excepthook
1200
1201 def set_custom_exc(self,exc_tuple,handler):
1178 def set_custom_exc(self,exc_tuple,handler):
1202 """set_custom_exc(exc_tuple,handler)
1179 """set_custom_exc(exc_tuple,handler)
1203
1180
1204 Set a custom exception handler, which will be called if any of the
1181 Set a custom exception handler, which will be called if any of the
1205 exceptions in exc_tuple occur in the mainloop (specifically, in the
1182 exceptions in exc_tuple occur in the mainloop (specifically, in the
1206 runcode() method.
1183 runcode() method.
1207
1184
1208 Inputs:
1185 Inputs:
1209
1186
1210 - exc_tuple: a *tuple* of valid exceptions to call the defined
1187 - exc_tuple: a *tuple* of valid exceptions to call the defined
1211 handler for. It is very important that you use a tuple, and NOT A
1188 handler for. It is very important that you use a tuple, and NOT A
1212 LIST here, because of the way Python's except statement works. If
1189 LIST here, because of the way Python's except statement works. If
1213 you only want to trap a single exception, use a singleton tuple:
1190 you only want to trap a single exception, use a singleton tuple:
1214
1191
1215 exc_tuple == (MyCustomException,)
1192 exc_tuple == (MyCustomException,)
1216
1193
1217 - handler: this must be defined as a function with the following
1194 - handler: this must be defined as a function with the following
1218 basic interface: def my_handler(self,etype,value,tb).
1195 basic interface: def my_handler(self,etype,value,tb).
1219
1196
1220 This will be made into an instance method (via new.instancemethod)
1197 This will be made into an instance method (via new.instancemethod)
1221 of IPython itself, and it will be called if any of the exceptions
1198 of IPython itself, and it will be called if any of the exceptions
1222 listed in the exc_tuple are caught. If the handler is None, an
1199 listed in the exc_tuple are caught. If the handler is None, an
1223 internal basic one is used, which just prints basic info.
1200 internal basic one is used, which just prints basic info.
1224
1201
1225 WARNING: by putting in your own exception handler into IPython's main
1202 WARNING: by putting in your own exception handler into IPython's main
1226 execution loop, you run a very good chance of nasty crashes. This
1203 execution loop, you run a very good chance of nasty crashes. This
1227 facility should only be used if you really know what you are doing."""
1204 facility should only be used if you really know what you are doing."""
1228
1205
1229 assert type(exc_tuple)==type(()) , \
1206 assert type(exc_tuple)==type(()) , \
1230 "The custom exceptions must be given AS A TUPLE."
1207 "The custom exceptions must be given AS A TUPLE."
1231
1208
1232 def dummy_handler(self,etype,value,tb):
1209 def dummy_handler(self,etype,value,tb):
1233 print '*** Simple custom exception handler ***'
1210 print '*** Simple custom exception handler ***'
1234 print 'Exception type :',etype
1211 print 'Exception type :',etype
1235 print 'Exception value:',value
1212 print 'Exception value:',value
1236 print 'Traceback :',tb
1213 print 'Traceback :',tb
1237 print 'Source code :','\n'.join(self.buffer)
1214 print 'Source code :','\n'.join(self.buffer)
1238
1215
1239 if handler is None: handler = dummy_handler
1216 if handler is None: handler = dummy_handler
1240
1217
1241 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1218 self.CustomTB = new.instancemethod(handler,self,self.__class__)
1242 self.custom_exceptions = exc_tuple
1219 self.custom_exceptions = exc_tuple
1243
1220
1244 def excepthook(self, etype, value, tb):
1221 def excepthook(self, etype, value, tb):
1245 """One more defense for GUI apps that call sys.excepthook.
1222 """One more defense for GUI apps that call sys.excepthook.
1246
1223
1247 GUI frameworks like wxPython trap exceptions and call
1224 GUI frameworks like wxPython trap exceptions and call
1248 sys.excepthook themselves. I guess this is a feature that
1225 sys.excepthook themselves. I guess this is a feature that
1249 enables them to keep running after exceptions that would
1226 enables them to keep running after exceptions that would
1250 otherwise kill their mainloop. This is a bother for IPython
1227 otherwise kill their mainloop. This is a bother for IPython
1251 which excepts to catch all of the program exceptions with a try:
1228 which excepts to catch all of the program exceptions with a try:
1252 except: statement.
1229 except: statement.
1253
1230
1254 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1231 Normally, IPython sets sys.excepthook to a CrashHandler instance, so if
1255 any app directly invokes sys.excepthook, it will look to the user like
1232 any app directly invokes sys.excepthook, it will look to the user like
1256 IPython crashed. In order to work around this, we can disable the
1233 IPython crashed. In order to work around this, we can disable the
1257 CrashHandler and replace it with this excepthook instead, which prints a
1234 CrashHandler and replace it with this excepthook instead, which prints a
1258 regular traceback using our InteractiveTB. In this fashion, apps which
1235 regular traceback using our InteractiveTB. In this fashion, apps which
1259 call sys.excepthook will generate a regular-looking exception from
1236 call sys.excepthook will generate a regular-looking exception from
1260 IPython, and the CrashHandler will only be triggered by real IPython
1237 IPython, and the CrashHandler will only be triggered by real IPython
1261 crashes.
1238 crashes.
1262
1239
1263 This hook should be used sparingly, only in places which are not likely
1240 This hook should be used sparingly, only in places which are not likely
1264 to be true IPython errors.
1241 to be true IPython errors.
1265 """
1242 """
1266 self.showtraceback((etype,value,tb),tb_offset=0)
1243 self.showtraceback((etype,value,tb),tb_offset=0)
1267
1244
1268 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1245 def showtraceback(self,exc_tuple = None,filename=None,tb_offset=None):
1269 """Display the exception that just occurred.
1246 """Display the exception that just occurred.
1270
1247
1271 If nothing is known about the exception, this is the method which
1248 If nothing is known about the exception, this is the method which
1272 should be used throughout the code for presenting user tracebacks,
1249 should be used throughout the code for presenting user tracebacks,
1273 rather than directly invoking the InteractiveTB object.
1250 rather than directly invoking the InteractiveTB object.
1274
1251
1275 A specific showsyntaxerror() also exists, but this method can take
1252 A specific showsyntaxerror() also exists, but this method can take
1276 care of calling it if needed, so unless you are explicitly catching a
1253 care of calling it if needed, so unless you are explicitly catching a
1277 SyntaxError exception, don't try to analyze the stack manually and
1254 SyntaxError exception, don't try to analyze the stack manually and
1278 simply call this method."""
1255 simply call this method."""
1279
1256
1280
1257
1281 # Though this won't be called by syntax errors in the input line,
1258 # Though this won't be called by syntax errors in the input line,
1282 # there may be SyntaxError cases whith imported code.
1259 # there may be SyntaxError cases whith imported code.
1283
1260
1284 try:
1261 try:
1285 if exc_tuple is None:
1262 if exc_tuple is None:
1286 etype, value, tb = sys.exc_info()
1263 etype, value, tb = sys.exc_info()
1287 else:
1264 else:
1288 etype, value, tb = exc_tuple
1265 etype, value, tb = exc_tuple
1289
1266
1290 if etype is SyntaxError:
1267 if etype is SyntaxError:
1291 self.showsyntaxerror(filename)
1268 self.showsyntaxerror(filename)
1292 elif etype is UsageError:
1269 elif etype is UsageError:
1293 print "UsageError:", value
1270 print "UsageError:", value
1294 else:
1271 else:
1295 # WARNING: these variables are somewhat deprecated and not
1272 # WARNING: these variables are somewhat deprecated and not
1296 # necessarily safe to use in a threaded environment, but tools
1273 # necessarily safe to use in a threaded environment, but tools
1297 # like pdb depend on their existence, so let's set them. If we
1274 # like pdb depend on their existence, so let's set them. If we
1298 # find problems in the field, we'll need to revisit their use.
1275 # find problems in the field, we'll need to revisit their use.
1299 sys.last_type = etype
1276 sys.last_type = etype
1300 sys.last_value = value
1277 sys.last_value = value
1301 sys.last_traceback = tb
1278 sys.last_traceback = tb
1302
1279
1303 if etype in self.custom_exceptions:
1280 if etype in self.custom_exceptions:
1304 self.CustomTB(etype,value,tb)
1281 self.CustomTB(etype,value,tb)
1305 else:
1282 else:
1306 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1283 self.InteractiveTB(etype,value,tb,tb_offset=tb_offset)
1307 if self.InteractiveTB.call_pdb:
1284 if self.InteractiveTB.call_pdb:
1308 # pdb mucks up readline, fix it back
1285 # pdb mucks up readline, fix it back
1309 self.set_completer()
1286 self.set_completer()
1310 except KeyboardInterrupt:
1287 except KeyboardInterrupt:
1311 self.write("\nKeyboardInterrupt\n")
1288 self.write("\nKeyboardInterrupt\n")
1312
1289
1313 def showsyntaxerror(self, filename=None):
1290 def showsyntaxerror(self, filename=None):
1314 """Display the syntax error that just occurred.
1291 """Display the syntax error that just occurred.
1315
1292
1316 This doesn't display a stack trace because there isn't one.
1293 This doesn't display a stack trace because there isn't one.
1317
1294
1318 If a filename is given, it is stuffed in the exception instead
1295 If a filename is given, it is stuffed in the exception instead
1319 of what was there before (because Python's parser always uses
1296 of what was there before (because Python's parser always uses
1320 "<string>" when reading from a string).
1297 "<string>" when reading from a string).
1321 """
1298 """
1322 etype, value, last_traceback = sys.exc_info()
1299 etype, value, last_traceback = sys.exc_info()
1323
1300
1324 # See note about these variables in showtraceback() below
1301 # See note about these variables in showtraceback() below
1325 sys.last_type = etype
1302 sys.last_type = etype
1326 sys.last_value = value
1303 sys.last_value = value
1327 sys.last_traceback = last_traceback
1304 sys.last_traceback = last_traceback
1328
1305
1329 if filename and etype is SyntaxError:
1306 if filename and etype is SyntaxError:
1330 # Work hard to stuff the correct filename in the exception
1307 # Work hard to stuff the correct filename in the exception
1331 try:
1308 try:
1332 msg, (dummy_filename, lineno, offset, line) = value
1309 msg, (dummy_filename, lineno, offset, line) = value
1333 except:
1310 except:
1334 # Not the format we expect; leave it alone
1311 # Not the format we expect; leave it alone
1335 pass
1312 pass
1336 else:
1313 else:
1337 # Stuff in the right filename
1314 # Stuff in the right filename
1338 try:
1315 try:
1339 # Assume SyntaxError is a class exception
1316 # Assume SyntaxError is a class exception
1340 value = SyntaxError(msg, (filename, lineno, offset, line))
1317 value = SyntaxError(msg, (filename, lineno, offset, line))
1341 except:
1318 except:
1342 # If that failed, assume SyntaxError is a string
1319 # If that failed, assume SyntaxError is a string
1343 value = msg, (filename, lineno, offset, line)
1320 value = msg, (filename, lineno, offset, line)
1344 self.SyntaxTB(etype,value,[])
1321 self.SyntaxTB(etype,value,[])
1345
1322
1346 def edit_syntax_error(self):
1323 def edit_syntax_error(self):
1347 """The bottom half of the syntax error handler called in the main loop.
1324 """The bottom half of the syntax error handler called in the main loop.
1348
1325
1349 Loop until syntax error is fixed or user cancels.
1326 Loop until syntax error is fixed or user cancels.
1350 """
1327 """
1351
1328
1352 while self.SyntaxTB.last_syntax_error:
1329 while self.SyntaxTB.last_syntax_error:
1353 # copy and clear last_syntax_error
1330 # copy and clear last_syntax_error
1354 err = self.SyntaxTB.clear_err_state()
1331 err = self.SyntaxTB.clear_err_state()
1355 if not self._should_recompile(err):
1332 if not self._should_recompile(err):
1356 return
1333 return
1357 try:
1334 try:
1358 # may set last_syntax_error again if a SyntaxError is raised
1335 # may set last_syntax_error again if a SyntaxError is raised
1359 self.safe_execfile(err.filename,self.user_ns)
1336 self.safe_execfile(err.filename,self.user_ns)
1360 except:
1337 except:
1361 self.showtraceback()
1338 self.showtraceback()
1362 else:
1339 else:
1363 try:
1340 try:
1364 f = file(err.filename)
1341 f = file(err.filename)
1365 try:
1342 try:
1366 # This should be inside a display_trap block and I
1343 # This should be inside a display_trap block and I
1367 # think it is.
1344 # think it is.
1368 sys.displayhook(f.read())
1345 sys.displayhook(f.read())
1369 finally:
1346 finally:
1370 f.close()
1347 f.close()
1371 except:
1348 except:
1372 self.showtraceback()
1349 self.showtraceback()
1373
1350
1374 def _should_recompile(self,e):
1351 def _should_recompile(self,e):
1375 """Utility routine for edit_syntax_error"""
1352 """Utility routine for edit_syntax_error"""
1376
1353
1377 if e.filename in ('<ipython console>','<input>','<string>',
1354 if e.filename in ('<ipython console>','<input>','<string>',
1378 '<console>','<BackgroundJob compilation>',
1355 '<console>','<BackgroundJob compilation>',
1379 None):
1356 None):
1380
1357
1381 return False
1358 return False
1382 try:
1359 try:
1383 if (self.autoedit_syntax and
1360 if (self.autoedit_syntax and
1384 not self.ask_yes_no('Return to editor to correct syntax error? '
1361 not self.ask_yes_no('Return to editor to correct syntax error? '
1385 '[Y/n] ','y')):
1362 '[Y/n] ','y')):
1386 return False
1363 return False
1387 except EOFError:
1364 except EOFError:
1388 return False
1365 return False
1389
1366
1390 def int0(x):
1367 def int0(x):
1391 try:
1368 try:
1392 return int(x)
1369 return int(x)
1393 except TypeError:
1370 except TypeError:
1394 return 0
1371 return 0
1395 # always pass integer line and offset values to editor hook
1372 # always pass integer line and offset values to editor hook
1396 try:
1373 try:
1397 self.hooks.fix_error_editor(e.filename,
1374 self.hooks.fix_error_editor(e.filename,
1398 int0(e.lineno),int0(e.offset),e.msg)
1375 int0(e.lineno),int0(e.offset),e.msg)
1399 except TryNext:
1376 except TryNext:
1400 warn('Could not open editor')
1377 warn('Could not open editor')
1401 return False
1378 return False
1402 return True
1379 return True
1403
1380
1404 #-------------------------------------------------------------------------
1381 #-------------------------------------------------------------------------
1405 # Things related to tab completion
1382 # Things related to tab completion
1406 #-------------------------------------------------------------------------
1383 #-------------------------------------------------------------------------
1407
1384
1408 def complete(self, text):
1385 def complete(self, text):
1409 """Return a sorted list of all possible completions on text.
1386 """Return a sorted list of all possible completions on text.
1410
1387
1411 Inputs:
1388 Inputs:
1412
1389
1413 - text: a string of text to be completed on.
1390 - text: a string of text to be completed on.
1414
1391
1415 This is a wrapper around the completion mechanism, similar to what
1392 This is a wrapper around the completion mechanism, similar to what
1416 readline does at the command line when the TAB key is hit. By
1393 readline does at the command line when the TAB key is hit. By
1417 exposing it as a method, it can be used by other non-readline
1394 exposing it as a method, it can be used by other non-readline
1418 environments (such as GUIs) for text completion.
1395 environments (such as GUIs) for text completion.
1419
1396
1420 Simple usage example:
1397 Simple usage example:
1421
1398
1422 In [7]: x = 'hello'
1399 In [7]: x = 'hello'
1423
1400
1424 In [8]: x
1401 In [8]: x
1425 Out[8]: 'hello'
1402 Out[8]: 'hello'
1426
1403
1427 In [9]: print x
1404 In [9]: print x
1428 hello
1405 hello
1429
1406
1430 In [10]: _ip.complete('x.l')
1407 In [10]: _ip.complete('x.l')
1431 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1408 Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1432 """
1409 """
1433
1410
1434 # Inject names into __builtin__ so we can complete on the added names.
1411 # Inject names into __builtin__ so we can complete on the added names.
1435 with self.builtin_trap:
1412 with self.builtin_trap:
1436 complete = self.Completer.complete
1413 complete = self.Completer.complete
1437 state = 0
1414 state = 0
1438 # use a dict so we get unique keys, since ipyhton's multiple
1415 # use a dict so we get unique keys, since ipyhton's multiple
1439 # completers can return duplicates. When we make 2.4 a requirement,
1416 # completers can return duplicates. When we make 2.4 a requirement,
1440 # start using sets instead, which are faster.
1417 # start using sets instead, which are faster.
1441 comps = {}
1418 comps = {}
1442 while True:
1419 while True:
1443 newcomp = complete(text,state,line_buffer=text)
1420 newcomp = complete(text,state,line_buffer=text)
1444 if newcomp is None:
1421 if newcomp is None:
1445 break
1422 break
1446 comps[newcomp] = 1
1423 comps[newcomp] = 1
1447 state += 1
1424 state += 1
1448 outcomps = comps.keys()
1425 outcomps = comps.keys()
1449 outcomps.sort()
1426 outcomps.sort()
1450 #print "T:",text,"OC:",outcomps # dbg
1427 #print "T:",text,"OC:",outcomps # dbg
1451 #print "vars:",self.user_ns.keys()
1428 #print "vars:",self.user_ns.keys()
1452 return outcomps
1429 return outcomps
1453
1430
1454 def set_custom_completer(self,completer,pos=0):
1431 def set_custom_completer(self,completer,pos=0):
1455 """Adds a new custom completer function.
1432 """Adds a new custom completer function.
1456
1433
1457 The position argument (defaults to 0) is the index in the completers
1434 The position argument (defaults to 0) is the index in the completers
1458 list where you want the completer to be inserted."""
1435 list where you want the completer to be inserted."""
1459
1436
1460 newcomp = new.instancemethod(completer,self.Completer,
1437 newcomp = new.instancemethod(completer,self.Completer,
1461 self.Completer.__class__)
1438 self.Completer.__class__)
1462 self.Completer.matchers.insert(pos,newcomp)
1439 self.Completer.matchers.insert(pos,newcomp)
1463
1440
1464 def set_completer(self):
1441 def set_completer(self):
1465 """Reset readline's completer to be our own."""
1442 """Reset readline's completer to be our own."""
1466 self.readline.set_completer(self.Completer.complete)
1443 self.readline.set_completer(self.Completer.complete)
1467
1444
1468 def set_completer_frame(self, frame=None):
1445 def set_completer_frame(self, frame=None):
1469 """Set the frame of the completer."""
1446 """Set the frame of the completer."""
1470 if frame:
1447 if frame:
1471 self.Completer.namespace = frame.f_locals
1448 self.Completer.namespace = frame.f_locals
1472 self.Completer.global_namespace = frame.f_globals
1449 self.Completer.global_namespace = frame.f_globals
1473 else:
1450 else:
1474 self.Completer.namespace = self.user_ns
1451 self.Completer.namespace = self.user_ns
1475 self.Completer.global_namespace = self.user_global_ns
1452 self.Completer.global_namespace = self.user_global_ns
1476
1453
1477 #-------------------------------------------------------------------------
1454 #-------------------------------------------------------------------------
1478 # Things related to readline
1455 # Things related to readline
1479 #-------------------------------------------------------------------------
1456 #-------------------------------------------------------------------------
1480
1457
1481 def init_readline(self):
1458 def init_readline(self):
1482 """Command history completion/saving/reloading."""
1459 """Command history completion/saving/reloading."""
1483
1460
1484 if self.readline_use:
1461 if self.readline_use:
1485 import IPython.utils.rlineimpl as readline
1462 import IPython.utils.rlineimpl as readline
1486
1463
1487 self.rl_next_input = None
1464 self.rl_next_input = None
1488 self.rl_do_indent = False
1465 self.rl_do_indent = False
1489
1466
1490 if not self.readline_use or not readline.have_readline:
1467 if not self.readline_use or not readline.have_readline:
1491 self.has_readline = False
1468 self.has_readline = False
1492 self.readline = None
1469 self.readline = None
1493 # Set a number of methods that depend on readline to be no-op
1470 # Set a number of methods that depend on readline to be no-op
1494 self.savehist = no_op
1471 self.savehist = no_op
1495 self.reloadhist = no_op
1472 self.reloadhist = no_op
1496 self.set_completer = no_op
1473 self.set_completer = no_op
1497 self.set_custom_completer = no_op
1474 self.set_custom_completer = no_op
1498 self.set_completer_frame = no_op
1475 self.set_completer_frame = no_op
1499 warn('Readline services not available or not loaded.')
1476 warn('Readline services not available or not loaded.')
1500 else:
1477 else:
1501 self.has_readline = True
1478 self.has_readline = True
1502 self.readline = readline
1479 self.readline = readline
1503 sys.modules['readline'] = readline
1480 sys.modules['readline'] = readline
1504 import atexit
1481 import atexit
1505 from IPython.core.completer import IPCompleter
1482 from IPython.core.completer import IPCompleter
1506 self.Completer = IPCompleter(self,
1483 self.Completer = IPCompleter(self,
1507 self.user_ns,
1484 self.user_ns,
1508 self.user_global_ns,
1485 self.user_global_ns,
1509 self.readline_omit__names,
1486 self.readline_omit__names,
1510 self.alias_manager.alias_table)
1487 self.alias_manager.alias_table)
1511 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1488 sdisp = self.strdispatchers.get('complete_command', StrDispatch())
1512 self.strdispatchers['complete_command'] = sdisp
1489 self.strdispatchers['complete_command'] = sdisp
1513 self.Completer.custom_completers = sdisp
1490 self.Completer.custom_completers = sdisp
1514 # Platform-specific configuration
1491 # Platform-specific configuration
1515 if os.name == 'nt':
1492 if os.name == 'nt':
1516 self.readline_startup_hook = readline.set_pre_input_hook
1493 self.readline_startup_hook = readline.set_pre_input_hook
1517 else:
1494 else:
1518 self.readline_startup_hook = readline.set_startup_hook
1495 self.readline_startup_hook = readline.set_startup_hook
1519
1496
1520 # Load user's initrc file (readline config)
1497 # Load user's initrc file (readline config)
1521 # Or if libedit is used, load editrc.
1498 # Or if libedit is used, load editrc.
1522 inputrc_name = os.environ.get('INPUTRC')
1499 inputrc_name = os.environ.get('INPUTRC')
1523 if inputrc_name is None:
1500 if inputrc_name is None:
1524 home_dir = get_home_dir()
1501 home_dir = get_home_dir()
1525 if home_dir is not None:
1502 if home_dir is not None:
1526 inputrc_name = '.inputrc'
1503 inputrc_name = '.inputrc'
1527 if readline.uses_libedit:
1504 if readline.uses_libedit:
1528 inputrc_name = '.editrc'
1505 inputrc_name = '.editrc'
1529 inputrc_name = os.path.join(home_dir, inputrc_name)
1506 inputrc_name = os.path.join(home_dir, inputrc_name)
1530 if os.path.isfile(inputrc_name):
1507 if os.path.isfile(inputrc_name):
1531 try:
1508 try:
1532 readline.read_init_file(inputrc_name)
1509 readline.read_init_file(inputrc_name)
1533 except:
1510 except:
1534 warn('Problems reading readline initialization file <%s>'
1511 warn('Problems reading readline initialization file <%s>'
1535 % inputrc_name)
1512 % inputrc_name)
1536
1513
1537 # save this in sys so embedded copies can restore it properly
1514 # save this in sys so embedded copies can restore it properly
1538 sys.ipcompleter = self.Completer.complete
1515 sys.ipcompleter = self.Completer.complete
1539 self.set_completer()
1516 self.set_completer()
1540
1517
1541 # Configure readline according to user's prefs
1518 # Configure readline according to user's prefs
1542 # This is only done if GNU readline is being used. If libedit
1519 # This is only done if GNU readline is being used. If libedit
1543 # is being used (as on Leopard) the readline config is
1520 # is being used (as on Leopard) the readline config is
1544 # not run as the syntax for libedit is different.
1521 # not run as the syntax for libedit is different.
1545 if not readline.uses_libedit:
1522 if not readline.uses_libedit:
1546 for rlcommand in self.readline_parse_and_bind:
1523 for rlcommand in self.readline_parse_and_bind:
1547 #print "loading rl:",rlcommand # dbg
1524 #print "loading rl:",rlcommand # dbg
1548 readline.parse_and_bind(rlcommand)
1525 readline.parse_and_bind(rlcommand)
1549
1526
1550 # Remove some chars from the delimiters list. If we encounter
1527 # Remove some chars from the delimiters list. If we encounter
1551 # unicode chars, discard them.
1528 # unicode chars, discard them.
1552 delims = readline.get_completer_delims().encode("ascii", "ignore")
1529 delims = readline.get_completer_delims().encode("ascii", "ignore")
1553 delims = delims.translate(string._idmap,
1530 delims = delims.translate(string._idmap,
1554 self.readline_remove_delims)
1531 self.readline_remove_delims)
1555 readline.set_completer_delims(delims)
1532 readline.set_completer_delims(delims)
1556 # otherwise we end up with a monster history after a while:
1533 # otherwise we end up with a monster history after a while:
1557 readline.set_history_length(1000)
1534 readline.set_history_length(1000)
1558 try:
1535 try:
1559 #print '*** Reading readline history' # dbg
1536 #print '*** Reading readline history' # dbg
1560 readline.read_history_file(self.histfile)
1537 readline.read_history_file(self.histfile)
1561 except IOError:
1538 except IOError:
1562 pass # It doesn't exist yet.
1539 pass # It doesn't exist yet.
1563
1540
1564 atexit.register(self.atexit_operations)
1541 atexit.register(self.atexit_operations)
1565 del atexit
1542 del atexit
1566
1543
1567 # Configure auto-indent for all platforms
1544 # Configure auto-indent for all platforms
1568 self.set_autoindent(self.autoindent)
1545 self.set_autoindent(self.autoindent)
1569
1546
1570 def set_next_input(self, s):
1547 def set_next_input(self, s):
1571 """ Sets the 'default' input string for the next command line.
1548 """ Sets the 'default' input string for the next command line.
1572
1549
1573 Requires readline.
1550 Requires readline.
1574
1551
1575 Example:
1552 Example:
1576
1553
1577 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1554 [D:\ipython]|1> _ip.set_next_input("Hello Word")
1578 [D:\ipython]|2> Hello Word_ # cursor is here
1555 [D:\ipython]|2> Hello Word_ # cursor is here
1579 """
1556 """
1580
1557
1581 self.rl_next_input = s
1558 self.rl_next_input = s
1582
1559
1583 def pre_readline(self):
1560 def pre_readline(self):
1584 """readline hook to be used at the start of each line.
1561 """readline hook to be used at the start of each line.
1585
1562
1586 Currently it handles auto-indent only."""
1563 Currently it handles auto-indent only."""
1587
1564
1588 #debugx('self.indent_current_nsp','pre_readline:')
1565 #debugx('self.indent_current_nsp','pre_readline:')
1589
1566
1590 if self.rl_do_indent:
1567 if self.rl_do_indent:
1591 self.readline.insert_text(self._indent_current_str())
1568 self.readline.insert_text(self._indent_current_str())
1592 if self.rl_next_input is not None:
1569 if self.rl_next_input is not None:
1593 self.readline.insert_text(self.rl_next_input)
1570 self.readline.insert_text(self.rl_next_input)
1594 self.rl_next_input = None
1571 self.rl_next_input = None
1595
1572
1596 def _indent_current_str(self):
1573 def _indent_current_str(self):
1597 """return the current level of indentation as a string"""
1574 """return the current level of indentation as a string"""
1598 return self.indent_current_nsp * ' '
1575 return self.indent_current_nsp * ' '
1599
1576
1600 #-------------------------------------------------------------------------
1577 #-------------------------------------------------------------------------
1601 # Things related to magics
1578 # Things related to magics
1602 #-------------------------------------------------------------------------
1579 #-------------------------------------------------------------------------
1603
1580
1604 def init_magics(self):
1581 def init_magics(self):
1605 # Set user colors (don't do it in the constructor above so that it
1582 # Set user colors (don't do it in the constructor above so that it
1606 # doesn't crash if colors option is invalid)
1583 # doesn't crash if colors option is invalid)
1607 self.magic_colors(self.colors)
1584 self.magic_colors(self.colors)
1608
1585
1609 def magic(self,arg_s):
1586 def magic(self,arg_s):
1610 """Call a magic function by name.
1587 """Call a magic function by name.
1611
1588
1612 Input: a string containing the name of the magic function to call and any
1589 Input: a string containing the name of the magic function to call and any
1613 additional arguments to be passed to the magic.
1590 additional arguments to be passed to the magic.
1614
1591
1615 magic('name -opt foo bar') is equivalent to typing at the ipython
1592 magic('name -opt foo bar') is equivalent to typing at the ipython
1616 prompt:
1593 prompt:
1617
1594
1618 In[1]: %name -opt foo bar
1595 In[1]: %name -opt foo bar
1619
1596
1620 To call a magic without arguments, simply use magic('name').
1597 To call a magic without arguments, simply use magic('name').
1621
1598
1622 This provides a proper Python function to call IPython's magics in any
1599 This provides a proper Python function to call IPython's magics in any
1623 valid Python code you can type at the interpreter, including loops and
1600 valid Python code you can type at the interpreter, including loops and
1624 compound statements.
1601 compound statements.
1625 """
1602 """
1626
1603
1627 args = arg_s.split(' ',1)
1604 args = arg_s.split(' ',1)
1628 magic_name = args[0]
1605 magic_name = args[0]
1629 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1606 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
1630
1607
1631 try:
1608 try:
1632 magic_args = args[1]
1609 magic_args = args[1]
1633 except IndexError:
1610 except IndexError:
1634 magic_args = ''
1611 magic_args = ''
1635 fn = getattr(self,'magic_'+magic_name,None)
1612 fn = getattr(self,'magic_'+magic_name,None)
1636 if fn is None:
1613 if fn is None:
1637 error("Magic function `%s` not found." % magic_name)
1614 error("Magic function `%s` not found." % magic_name)
1638 else:
1615 else:
1639 magic_args = self.var_expand(magic_args,1)
1616 magic_args = self.var_expand(magic_args,1)
1640 with nested(self.builtin_trap,):
1617 with nested(self.builtin_trap,):
1641 result = fn(magic_args)
1618 result = fn(magic_args)
1642 return result
1619 return result
1643
1620
1644 def define_magic(self, magicname, func):
1621 def define_magic(self, magicname, func):
1645 """Expose own function as magic function for ipython
1622 """Expose own function as magic function for ipython
1646
1623
1647 def foo_impl(self,parameter_s=''):
1624 def foo_impl(self,parameter_s=''):
1648 'My very own magic!. (Use docstrings, IPython reads them).'
1625 'My very own magic!. (Use docstrings, IPython reads them).'
1649 print 'Magic function. Passed parameter is between < >:'
1626 print 'Magic function. Passed parameter is between < >:'
1650 print '<%s>' % parameter_s
1627 print '<%s>' % parameter_s
1651 print 'The self object is:',self
1628 print 'The self object is:',self
1652
1629
1653 self.define_magic('foo',foo_impl)
1630 self.define_magic('foo',foo_impl)
1654 """
1631 """
1655
1632
1656 import new
1633 import new
1657 im = new.instancemethod(func,self, self.__class__)
1634 im = new.instancemethod(func,self, self.__class__)
1658 old = getattr(self, "magic_" + magicname, None)
1635 old = getattr(self, "magic_" + magicname, None)
1659 setattr(self, "magic_" + magicname, im)
1636 setattr(self, "magic_" + magicname, im)
1660 return old
1637 return old
1661
1638
1662 #-------------------------------------------------------------------------
1639 #-------------------------------------------------------------------------
1663 # Things related to macros
1640 # Things related to macros
1664 #-------------------------------------------------------------------------
1641 #-------------------------------------------------------------------------
1665
1642
1666 def define_macro(self, name, themacro):
1643 def define_macro(self, name, themacro):
1667 """Define a new macro
1644 """Define a new macro
1668
1645
1669 Parameters
1646 Parameters
1670 ----------
1647 ----------
1671 name : str
1648 name : str
1672 The name of the macro.
1649 The name of the macro.
1673 themacro : str or Macro
1650 themacro : str or Macro
1674 The action to do upon invoking the macro. If a string, a new
1651 The action to do upon invoking the macro. If a string, a new
1675 Macro object is created by passing the string to it.
1652 Macro object is created by passing the string to it.
1676 """
1653 """
1677
1654
1678 from IPython.core import macro
1655 from IPython.core import macro
1679
1656
1680 if isinstance(themacro, basestring):
1657 if isinstance(themacro, basestring):
1681 themacro = macro.Macro(themacro)
1658 themacro = macro.Macro(themacro)
1682 if not isinstance(themacro, macro.Macro):
1659 if not isinstance(themacro, macro.Macro):
1683 raise ValueError('A macro must be a string or a Macro instance.')
1660 raise ValueError('A macro must be a string or a Macro instance.')
1684 self.user_ns[name] = themacro
1661 self.user_ns[name] = themacro
1685
1662
1686 #-------------------------------------------------------------------------
1663 #-------------------------------------------------------------------------
1687 # Things related to the running of system commands
1664 # Things related to the running of system commands
1688 #-------------------------------------------------------------------------
1665 #-------------------------------------------------------------------------
1689
1666
1690 def system(self, cmd):
1667 def system(self, cmd):
1691 """Make a system call, using IPython."""
1668 """Make a system call, using IPython."""
1692 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1669 return self.hooks.shell_hook(self.var_expand(cmd, depth=2))
1693
1670
1694 #-------------------------------------------------------------------------
1671 #-------------------------------------------------------------------------
1695 # Things related to aliases
1672 # Things related to aliases
1696 #-------------------------------------------------------------------------
1673 #-------------------------------------------------------------------------
1697
1674
1698 def init_alias(self):
1675 def init_alias(self):
1699 self.alias_manager = AliasManager(self, config=self.config)
1676 self.alias_manager = AliasManager(self, config=self.config)
1700 self.ns_table['alias'] = self.alias_manager.alias_table,
1677 self.ns_table['alias'] = self.alias_manager.alias_table,
1701
1678
1702 #-------------------------------------------------------------------------
1679 #-------------------------------------------------------------------------
1703 # Things related to the running of code
1680 # Things related to the running of code
1704 #-------------------------------------------------------------------------
1681 #-------------------------------------------------------------------------
1705
1682
1706 def ex(self, cmd):
1683 def ex(self, cmd):
1707 """Execute a normal python statement in user namespace."""
1684 """Execute a normal python statement in user namespace."""
1708 with nested(self.builtin_trap,):
1685 with nested(self.builtin_trap,):
1709 exec cmd in self.user_global_ns, self.user_ns
1686 exec cmd in self.user_global_ns, self.user_ns
1710
1687
1711 def ev(self, expr):
1688 def ev(self, expr):
1712 """Evaluate python expression expr in user namespace.
1689 """Evaluate python expression expr in user namespace.
1713
1690
1714 Returns the result of evaluation
1691 Returns the result of evaluation
1715 """
1692 """
1716 with nested(self.builtin_trap,):
1693 with nested(self.builtin_trap,):
1717 return eval(expr, self.user_global_ns, self.user_ns)
1694 return eval(expr, self.user_global_ns, self.user_ns)
1718
1695
1719 def mainloop(self, display_banner=None):
1696 def mainloop(self, display_banner=None):
1720 """Start the mainloop.
1697 """Start the mainloop.
1721
1698
1722 If an optional banner argument is given, it will override the
1699 If an optional banner argument is given, it will override the
1723 internally created default banner.
1700 internally created default banner.
1724 """
1701 """
1725
1702
1726 with nested(self.builtin_trap, self.display_trap):
1703 with nested(self.builtin_trap, self.display_trap):
1727
1704
1728 # if you run stuff with -c <cmd>, raw hist is not updated
1705 # if you run stuff with -c <cmd>, raw hist is not updated
1729 # ensure that it's in sync
1706 # ensure that it's in sync
1730 if len(self.input_hist) != len (self.input_hist_raw):
1707 if len(self.input_hist) != len (self.input_hist_raw):
1731 self.input_hist_raw = InputList(self.input_hist)
1708 self.input_hist_raw = InputList(self.input_hist)
1732
1709
1733 while 1:
1710 while 1:
1734 try:
1711 try:
1735 self.interact(display_banner=display_banner)
1712 self.interact(display_banner=display_banner)
1736 #self.interact_with_readline()
1713 #self.interact_with_readline()
1737 # XXX for testing of a readline-decoupled repl loop, call
1714 # XXX for testing of a readline-decoupled repl loop, call
1738 # interact_with_readline above
1715 # interact_with_readline above
1739 break
1716 break
1740 except KeyboardInterrupt:
1717 except KeyboardInterrupt:
1741 # this should not be necessary, but KeyboardInterrupt
1718 # this should not be necessary, but KeyboardInterrupt
1742 # handling seems rather unpredictable...
1719 # handling seems rather unpredictable...
1743 self.write("\nKeyboardInterrupt in interact()\n")
1720 self.write("\nKeyboardInterrupt in interact()\n")
1744
1721
1745 def interact_prompt(self):
1722 def interact_prompt(self):
1746 """ Print the prompt (in read-eval-print loop)
1723 """ Print the prompt (in read-eval-print loop)
1747
1724
1748 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1725 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1749 used in standard IPython flow.
1726 used in standard IPython flow.
1750 """
1727 """
1751 if self.more:
1728 if self.more:
1752 try:
1729 try:
1753 prompt = self.hooks.generate_prompt(True)
1730 prompt = self.hooks.generate_prompt(True)
1754 except:
1731 except:
1755 self.showtraceback()
1732 self.showtraceback()
1756 if self.autoindent:
1733 if self.autoindent:
1757 self.rl_do_indent = True
1734 self.rl_do_indent = True
1758
1735
1759 else:
1736 else:
1760 try:
1737 try:
1761 prompt = self.hooks.generate_prompt(False)
1738 prompt = self.hooks.generate_prompt(False)
1762 except:
1739 except:
1763 self.showtraceback()
1740 self.showtraceback()
1764 self.write(prompt)
1741 self.write(prompt)
1765
1742
1766 def interact_handle_input(self,line):
1743 def interact_handle_input(self,line):
1767 """ Handle the input line (in read-eval-print loop)
1744 """ Handle the input line (in read-eval-print loop)
1768
1745
1769 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1746 Provided for those who want to implement their own read-eval-print loop (e.g. GUIs), not
1770 used in standard IPython flow.
1747 used in standard IPython flow.
1771 """
1748 """
1772 if line.lstrip() == line:
1749 if line.lstrip() == line:
1773 self.shadowhist.add(line.strip())
1750 self.shadowhist.add(line.strip())
1774 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1751 lineout = self.prefilter_manager.prefilter_lines(line,self.more)
1775
1752
1776 if line.strip():
1753 if line.strip():
1777 if self.more:
1754 if self.more:
1778 self.input_hist_raw[-1] += '%s\n' % line
1755 self.input_hist_raw[-1] += '%s\n' % line
1779 else:
1756 else:
1780 self.input_hist_raw.append('%s\n' % line)
1757 self.input_hist_raw.append('%s\n' % line)
1781
1758
1782
1759
1783 self.more = self.push_line(lineout)
1760 self.more = self.push_line(lineout)
1784 if (self.SyntaxTB.last_syntax_error and
1761 if (self.SyntaxTB.last_syntax_error and
1785 self.autoedit_syntax):
1762 self.autoedit_syntax):
1786 self.edit_syntax_error()
1763 self.edit_syntax_error()
1787
1764
1788 def interact_with_readline(self):
1765 def interact_with_readline(self):
1789 """ Demo of using interact_handle_input, interact_prompt
1766 """ Demo of using interact_handle_input, interact_prompt
1790
1767
1791 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1768 This is the main read-eval-print loop. If you need to implement your own (e.g. for GUI),
1792 it should work like this.
1769 it should work like this.
1793 """
1770 """
1794 self.readline_startup_hook(self.pre_readline)
1771 self.readline_startup_hook(self.pre_readline)
1795 while not self.exit_now:
1772 while not self.exit_now:
1796 self.interact_prompt()
1773 self.interact_prompt()
1797 if self.more:
1774 if self.more:
1798 self.rl_do_indent = True
1775 self.rl_do_indent = True
1799 else:
1776 else:
1800 self.rl_do_indent = False
1777 self.rl_do_indent = False
1801 line = raw_input_original().decode(self.stdin_encoding)
1778 line = raw_input_original().decode(self.stdin_encoding)
1802 self.interact_handle_input(line)
1779 self.interact_handle_input(line)
1803
1780
1804 def interact(self, display_banner=None):
1781 def interact(self, display_banner=None):
1805 """Closely emulate the interactive Python console."""
1782 """Closely emulate the interactive Python console."""
1806
1783
1807 # batch run -> do not interact
1784 # batch run -> do not interact
1808 if self.exit_now:
1785 if self.exit_now:
1809 return
1786 return
1810
1787
1811 if display_banner is None:
1788 if display_banner is None:
1812 display_banner = self.display_banner
1789 display_banner = self.display_banner
1813 if display_banner:
1790 if display_banner:
1814 self.show_banner()
1791 self.show_banner()
1815
1792
1816 more = 0
1793 more = 0
1817
1794
1818 # Mark activity in the builtins
1795 # Mark activity in the builtins
1819 __builtin__.__dict__['__IPYTHON__active'] += 1
1796 __builtin__.__dict__['__IPYTHON__active'] += 1
1820
1797
1821 if self.has_readline:
1798 if self.has_readline:
1822 self.readline_startup_hook(self.pre_readline)
1799 self.readline_startup_hook(self.pre_readline)
1823 # exit_now is set by a call to %Exit or %Quit, through the
1800 # exit_now is set by a call to %Exit or %Quit, through the
1824 # ask_exit callback.
1801 # ask_exit callback.
1825
1802
1826 while not self.exit_now:
1803 while not self.exit_now:
1827 self.hooks.pre_prompt_hook()
1804 self.hooks.pre_prompt_hook()
1828 if more:
1805 if more:
1829 try:
1806 try:
1830 prompt = self.hooks.generate_prompt(True)
1807 prompt = self.hooks.generate_prompt(True)
1831 except:
1808 except:
1832 self.showtraceback()
1809 self.showtraceback()
1833 if self.autoindent:
1810 if self.autoindent:
1834 self.rl_do_indent = True
1811 self.rl_do_indent = True
1835
1812
1836 else:
1813 else:
1837 try:
1814 try:
1838 prompt = self.hooks.generate_prompt(False)
1815 prompt = self.hooks.generate_prompt(False)
1839 except:
1816 except:
1840 self.showtraceback()
1817 self.showtraceback()
1841 try:
1818 try:
1842 line = self.raw_input(prompt, more)
1819 line = self.raw_input(prompt, more)
1843 if self.exit_now:
1820 if self.exit_now:
1844 # quick exit on sys.std[in|out] close
1821 # quick exit on sys.std[in|out] close
1845 break
1822 break
1846 if self.autoindent:
1823 if self.autoindent:
1847 self.rl_do_indent = False
1824 self.rl_do_indent = False
1848
1825
1849 except KeyboardInterrupt:
1826 except KeyboardInterrupt:
1850 #double-guard against keyboardinterrupts during kbdint handling
1827 #double-guard against keyboardinterrupts during kbdint handling
1851 try:
1828 try:
1852 self.write('\nKeyboardInterrupt\n')
1829 self.write('\nKeyboardInterrupt\n')
1853 self.resetbuffer()
1830 self.resetbuffer()
1854 # keep cache in sync with the prompt counter:
1831 # keep cache in sync with the prompt counter:
1855 self.outputcache.prompt_count -= 1
1832 self.outputcache.prompt_count -= 1
1856
1833
1857 if self.autoindent:
1834 if self.autoindent:
1858 self.indent_current_nsp = 0
1835 self.indent_current_nsp = 0
1859 more = 0
1836 more = 0
1860 except KeyboardInterrupt:
1837 except KeyboardInterrupt:
1861 pass
1838 pass
1862 except EOFError:
1839 except EOFError:
1863 if self.autoindent:
1840 if self.autoindent:
1864 self.rl_do_indent = False
1841 self.rl_do_indent = False
1865 if self.has_readline:
1842 if self.has_readline:
1866 self.readline_startup_hook(None)
1843 self.readline_startup_hook(None)
1867 self.write('\n')
1844 self.write('\n')
1868 self.exit()
1845 self.exit()
1869 except bdb.BdbQuit:
1846 except bdb.BdbQuit:
1870 warn('The Python debugger has exited with a BdbQuit exception.\n'
1847 warn('The Python debugger has exited with a BdbQuit exception.\n'
1871 'Because of how pdb handles the stack, it is impossible\n'
1848 'Because of how pdb handles the stack, it is impossible\n'
1872 'for IPython to properly format this particular exception.\n'
1849 'for IPython to properly format this particular exception.\n'
1873 'IPython will resume normal operation.')
1850 'IPython will resume normal operation.')
1874 except:
1851 except:
1875 # exceptions here are VERY RARE, but they can be triggered
1852 # exceptions here are VERY RARE, but they can be triggered
1876 # asynchronously by signal handlers, for example.
1853 # asynchronously by signal handlers, for example.
1877 self.showtraceback()
1854 self.showtraceback()
1878 else:
1855 else:
1879 more = self.push_line(line)
1856 more = self.push_line(line)
1880 if (self.SyntaxTB.last_syntax_error and
1857 if (self.SyntaxTB.last_syntax_error and
1881 self.autoedit_syntax):
1858 self.autoedit_syntax):
1882 self.edit_syntax_error()
1859 self.edit_syntax_error()
1883
1860
1884 # We are off again...
1861 # We are off again...
1885 __builtin__.__dict__['__IPYTHON__active'] -= 1
1862 __builtin__.__dict__['__IPYTHON__active'] -= 1
1886
1863
1887 def safe_execfile(self, fname, *where, **kw):
1864 def safe_execfile(self, fname, *where, **kw):
1888 """A safe version of the builtin execfile().
1865 """A safe version of the builtin execfile().
1889
1866
1890 This version will never throw an exception, but instead print
1867 This version will never throw an exception, but instead print
1891 helpful error messages to the screen. This only works on pure
1868 helpful error messages to the screen. This only works on pure
1892 Python files with the .py extension.
1869 Python files with the .py extension.
1893
1870
1894 Parameters
1871 Parameters
1895 ----------
1872 ----------
1896 fname : string
1873 fname : string
1897 The name of the file to be executed.
1874 The name of the file to be executed.
1898 where : tuple
1875 where : tuple
1899 One or two namespaces, passed to execfile() as (globals,locals).
1876 One or two namespaces, passed to execfile() as (globals,locals).
1900 If only one is given, it is passed as both.
1877 If only one is given, it is passed as both.
1901 exit_ignore : bool (False)
1878 exit_ignore : bool (False)
1902 If True, then don't print errors for non-zero exit statuses.
1879 If True, then don't print errors for non-zero exit statuses.
1903 """
1880 """
1904 kw.setdefault('exit_ignore', False)
1881 kw.setdefault('exit_ignore', False)
1905
1882
1906 fname = os.path.abspath(os.path.expanduser(fname))
1883 fname = os.path.abspath(os.path.expanduser(fname))
1907
1884
1908 # Make sure we have a .py file
1885 # Make sure we have a .py file
1909 if not fname.endswith('.py'):
1886 if not fname.endswith('.py'):
1910 warn('File must end with .py to be run using execfile: <%s>' % fname)
1887 warn('File must end with .py to be run using execfile: <%s>' % fname)
1911
1888
1912 # Make sure we can open the file
1889 # Make sure we can open the file
1913 try:
1890 try:
1914 with open(fname) as thefile:
1891 with open(fname) as thefile:
1915 pass
1892 pass
1916 except:
1893 except:
1917 warn('Could not open file <%s> for safe execution.' % fname)
1894 warn('Could not open file <%s> for safe execution.' % fname)
1918 return
1895 return
1919
1896
1920 # Find things also in current directory. This is needed to mimic the
1897 # Find things also in current directory. This is needed to mimic the
1921 # behavior of running a script from the system command line, where
1898 # behavior of running a script from the system command line, where
1922 # Python inserts the script's directory into sys.path
1899 # Python inserts the script's directory into sys.path
1923 dname = os.path.dirname(fname)
1900 dname = os.path.dirname(fname)
1924
1901
1925 with prepended_to_syspath(dname):
1902 with prepended_to_syspath(dname):
1926 try:
1903 try:
1927 if sys.platform == 'win32' and sys.version_info < (2,5,1):
1904 if sys.platform == 'win32' and sys.version_info < (2,5,1):
1928 # Work around a bug in Python for Windows. The bug was
1905 # Work around a bug in Python for Windows. The bug was
1929 # fixed in in Python 2.5 r54159 and 54158, but that's still
1906 # fixed in in Python 2.5 r54159 and 54158, but that's still
1930 # SVN Python as of March/07. For details, see:
1907 # SVN Python as of March/07. For details, see:
1931 # http://projects.scipy.org/ipython/ipython/ticket/123
1908 # http://projects.scipy.org/ipython/ipython/ticket/123
1932 try:
1909 try:
1933 globs,locs = where[0:2]
1910 globs,locs = where[0:2]
1934 except:
1911 except:
1935 try:
1912 try:
1936 globs = locs = where[0]
1913 globs = locs = where[0]
1937 except:
1914 except:
1938 globs = locs = globals()
1915 globs = locs = globals()
1939 exec file(fname) in globs,locs
1916 exec file(fname) in globs,locs
1940 else:
1917 else:
1941 execfile(fname,*where)
1918 execfile(fname,*where)
1942 except SyntaxError:
1919 except SyntaxError:
1943 self.showsyntaxerror()
1920 self.showsyntaxerror()
1944 warn('Failure executing file: <%s>' % fname)
1921 warn('Failure executing file: <%s>' % fname)
1945 except SystemExit, status:
1922 except SystemExit, status:
1946 # Code that correctly sets the exit status flag to success (0)
1923 # Code that correctly sets the exit status flag to success (0)
1947 # shouldn't be bothered with a traceback. Note that a plain
1924 # shouldn't be bothered with a traceback. Note that a plain
1948 # sys.exit() does NOT set the message to 0 (it's empty) so that
1925 # sys.exit() does NOT set the message to 0 (it's empty) so that
1949 # will still get a traceback. Note that the structure of the
1926 # will still get a traceback. Note that the structure of the
1950 # SystemExit exception changed between Python 2.4 and 2.5, so
1927 # SystemExit exception changed between Python 2.4 and 2.5, so
1951 # the checks must be done in a version-dependent way.
1928 # the checks must be done in a version-dependent way.
1952 show = False
1929 show = False
1953 if status.args[0]==0 and not kw['exit_ignore']:
1930 if status.args[0]==0 and not kw['exit_ignore']:
1954 show = True
1931 show = True
1955 if show:
1932 if show:
1956 self.showtraceback()
1933 self.showtraceback()
1957 warn('Failure executing file: <%s>' % fname)
1934 warn('Failure executing file: <%s>' % fname)
1958 except:
1935 except:
1959 self.showtraceback()
1936 self.showtraceback()
1960 warn('Failure executing file: <%s>' % fname)
1937 warn('Failure executing file: <%s>' % fname)
1961
1938
1962 def safe_execfile_ipy(self, fname):
1939 def safe_execfile_ipy(self, fname):
1963 """Like safe_execfile, but for .ipy files with IPython syntax.
1940 """Like safe_execfile, but for .ipy files with IPython syntax.
1964
1941
1965 Parameters
1942 Parameters
1966 ----------
1943 ----------
1967 fname : str
1944 fname : str
1968 The name of the file to execute. The filename must have a
1945 The name of the file to execute. The filename must have a
1969 .ipy extension.
1946 .ipy extension.
1970 """
1947 """
1971 fname = os.path.abspath(os.path.expanduser(fname))
1948 fname = os.path.abspath(os.path.expanduser(fname))
1972
1949
1973 # Make sure we have a .py file
1950 # Make sure we have a .py file
1974 if not fname.endswith('.ipy'):
1951 if not fname.endswith('.ipy'):
1975 warn('File must end with .py to be run using execfile: <%s>' % fname)
1952 warn('File must end with .py to be run using execfile: <%s>' % fname)
1976
1953
1977 # Make sure we can open the file
1954 # Make sure we can open the file
1978 try:
1955 try:
1979 with open(fname) as thefile:
1956 with open(fname) as thefile:
1980 pass
1957 pass
1981 except:
1958 except:
1982 warn('Could not open file <%s> for safe execution.' % fname)
1959 warn('Could not open file <%s> for safe execution.' % fname)
1983 return
1960 return
1984
1961
1985 # Find things also in current directory. This is needed to mimic the
1962 # Find things also in current directory. This is needed to mimic the
1986 # behavior of running a script from the system command line, where
1963 # behavior of running a script from the system command line, where
1987 # Python inserts the script's directory into sys.path
1964 # Python inserts the script's directory into sys.path
1988 dname = os.path.dirname(fname)
1965 dname = os.path.dirname(fname)
1989
1966
1990 with prepended_to_syspath(dname):
1967 with prepended_to_syspath(dname):
1991 try:
1968 try:
1992 with open(fname) as thefile:
1969 with open(fname) as thefile:
1993 script = thefile.read()
1970 script = thefile.read()
1994 # self.runlines currently captures all exceptions
1971 # self.runlines currently captures all exceptions
1995 # raise in user code. It would be nice if there were
1972 # raise in user code. It would be nice if there were
1996 # versions of runlines, execfile that did raise, so
1973 # versions of runlines, execfile that did raise, so
1997 # we could catch the errors.
1974 # we could catch the errors.
1998 self.runlines(script, clean=True)
1975 self.runlines(script, clean=True)
1999 except:
1976 except:
2000 self.showtraceback()
1977 self.showtraceback()
2001 warn('Unknown failure executing file: <%s>' % fname)
1978 warn('Unknown failure executing file: <%s>' % fname)
2002
1979
2003 def _is_secondary_block_start(self, s):
1980 def _is_secondary_block_start(self, s):
2004 if not s.endswith(':'):
1981 if not s.endswith(':'):
2005 return False
1982 return False
2006 if (s.startswith('elif') or
1983 if (s.startswith('elif') or
2007 s.startswith('else') or
1984 s.startswith('else') or
2008 s.startswith('except') or
1985 s.startswith('except') or
2009 s.startswith('finally')):
1986 s.startswith('finally')):
2010 return True
1987 return True
2011
1988
2012 def cleanup_ipy_script(self, script):
1989 def cleanup_ipy_script(self, script):
2013 """Make a script safe for self.runlines()
1990 """Make a script safe for self.runlines()
2014
1991
2015 Currently, IPython is lines based, with blocks being detected by
1992 Currently, IPython is lines based, with blocks being detected by
2016 empty lines. This is a problem for block based scripts that may
1993 empty lines. This is a problem for block based scripts that may
2017 not have empty lines after blocks. This script adds those empty
1994 not have empty lines after blocks. This script adds those empty
2018 lines to make scripts safe for running in the current line based
1995 lines to make scripts safe for running in the current line based
2019 IPython.
1996 IPython.
2020 """
1997 """
2021 res = []
1998 res = []
2022 lines = script.splitlines()
1999 lines = script.splitlines()
2023 level = 0
2000 level = 0
2024
2001
2025 for l in lines:
2002 for l in lines:
2026 lstripped = l.lstrip()
2003 lstripped = l.lstrip()
2027 stripped = l.strip()
2004 stripped = l.strip()
2028 if not stripped:
2005 if not stripped:
2029 continue
2006 continue
2030 newlevel = len(l) - len(lstripped)
2007 newlevel = len(l) - len(lstripped)
2031 if level > 0 and newlevel == 0 and \
2008 if level > 0 and newlevel == 0 and \
2032 not self._is_secondary_block_start(stripped):
2009 not self._is_secondary_block_start(stripped):
2033 # add empty line
2010 # add empty line
2034 res.append('')
2011 res.append('')
2035 res.append(l)
2012 res.append(l)
2036 level = newlevel
2013 level = newlevel
2037
2014
2038 return '\n'.join(res) + '\n'
2015 return '\n'.join(res) + '\n'
2039
2016
2040 def runlines(self, lines, clean=False):
2017 def runlines(self, lines, clean=False):
2041 """Run a string of one or more lines of source.
2018 """Run a string of one or more lines of source.
2042
2019
2043 This method is capable of running a string containing multiple source
2020 This method is capable of running a string containing multiple source
2044 lines, as if they had been entered at the IPython prompt. Since it
2021 lines, as if they had been entered at the IPython prompt. Since it
2045 exposes IPython's processing machinery, the given strings can contain
2022 exposes IPython's processing machinery, the given strings can contain
2046 magic calls (%magic), special shell access (!cmd), etc.
2023 magic calls (%magic), special shell access (!cmd), etc.
2047 """
2024 """
2048
2025
2049 if isinstance(lines, (list, tuple)):
2026 if isinstance(lines, (list, tuple)):
2050 lines = '\n'.join(lines)
2027 lines = '\n'.join(lines)
2051
2028
2052 if clean:
2029 if clean:
2053 lines = self.cleanup_ipy_script(lines)
2030 lines = self.cleanup_ipy_script(lines)
2054
2031
2055 # We must start with a clean buffer, in case this is run from an
2032 # We must start with a clean buffer, in case this is run from an
2056 # interactive IPython session (via a magic, for example).
2033 # interactive IPython session (via a magic, for example).
2057 self.resetbuffer()
2034 self.resetbuffer()
2058 lines = lines.splitlines()
2035 lines = lines.splitlines()
2059 more = 0
2036 more = 0
2060
2037
2061 with nested(self.builtin_trap, self.display_trap):
2038 with nested(self.builtin_trap, self.display_trap):
2062 for line in lines:
2039 for line in lines:
2063 # skip blank lines so we don't mess up the prompt counter, but do
2040 # skip blank lines so we don't mess up the prompt counter, but do
2064 # NOT skip even a blank line if we are in a code block (more is
2041 # NOT skip even a blank line if we are in a code block (more is
2065 # true)
2042 # true)
2066
2043
2067 if line or more:
2044 if line or more:
2068 # push to raw history, so hist line numbers stay in sync
2045 # push to raw history, so hist line numbers stay in sync
2069 self.input_hist_raw.append("# " + line + "\n")
2046 self.input_hist_raw.append("# " + line + "\n")
2070 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
2047 prefiltered = self.prefilter_manager.prefilter_lines(line,more)
2071 more = self.push_line(prefiltered)
2048 more = self.push_line(prefiltered)
2072 # IPython's runsource returns None if there was an error
2049 # IPython's runsource returns None if there was an error
2073 # compiling the code. This allows us to stop processing right
2050 # compiling the code. This allows us to stop processing right
2074 # away, so the user gets the error message at the right place.
2051 # away, so the user gets the error message at the right place.
2075 if more is None:
2052 if more is None:
2076 break
2053 break
2077 else:
2054 else:
2078 self.input_hist_raw.append("\n")
2055 self.input_hist_raw.append("\n")
2079 # final newline in case the input didn't have it, so that the code
2056 # final newline in case the input didn't have it, so that the code
2080 # actually does get executed
2057 # actually does get executed
2081 if more:
2058 if more:
2082 self.push_line('\n')
2059 self.push_line('\n')
2083
2060
2084 def runsource(self, source, filename='<input>', symbol='single'):
2061 def runsource(self, source, filename='<input>', symbol='single'):
2085 """Compile and run some source in the interpreter.
2062 """Compile and run some source in the interpreter.
2086
2063
2087 Arguments are as for compile_command().
2064 Arguments are as for compile_command().
2088
2065
2089 One several things can happen:
2066 One several things can happen:
2090
2067
2091 1) The input is incorrect; compile_command() raised an
2068 1) The input is incorrect; compile_command() raised an
2092 exception (SyntaxError or OverflowError). A syntax traceback
2069 exception (SyntaxError or OverflowError). A syntax traceback
2093 will be printed by calling the showsyntaxerror() method.
2070 will be printed by calling the showsyntaxerror() method.
2094
2071
2095 2) The input is incomplete, and more input is required;
2072 2) The input is incomplete, and more input is required;
2096 compile_command() returned None. Nothing happens.
2073 compile_command() returned None. Nothing happens.
2097
2074
2098 3) The input is complete; compile_command() returned a code
2075 3) The input is complete; compile_command() returned a code
2099 object. The code is executed by calling self.runcode() (which
2076 object. The code is executed by calling self.runcode() (which
2100 also handles run-time exceptions, except for SystemExit).
2077 also handles run-time exceptions, except for SystemExit).
2101
2078
2102 The return value is:
2079 The return value is:
2103
2080
2104 - True in case 2
2081 - True in case 2
2105
2082
2106 - False in the other cases, unless an exception is raised, where
2083 - False in the other cases, unless an exception is raised, where
2107 None is returned instead. This can be used by external callers to
2084 None is returned instead. This can be used by external callers to
2108 know whether to continue feeding input or not.
2085 know whether to continue feeding input or not.
2109
2086
2110 The return value can be used to decide whether to use sys.ps1 or
2087 The return value can be used to decide whether to use sys.ps1 or
2111 sys.ps2 to prompt the next line."""
2088 sys.ps2 to prompt the next line."""
2112
2089
2113 # if the source code has leading blanks, add 'if 1:\n' to it
2090 # if the source code has leading blanks, add 'if 1:\n' to it
2114 # this allows execution of indented pasted code. It is tempting
2091 # this allows execution of indented pasted code. It is tempting
2115 # to add '\n' at the end of source to run commands like ' a=1'
2092 # to add '\n' at the end of source to run commands like ' a=1'
2116 # directly, but this fails for more complicated scenarios
2093 # directly, but this fails for more complicated scenarios
2117 source=source.encode(self.stdin_encoding)
2094 source=source.encode(self.stdin_encoding)
2118 if source[:1] in [' ', '\t']:
2095 if source[:1] in [' ', '\t']:
2119 source = 'if 1:\n%s' % source
2096 source = 'if 1:\n%s' % source
2120
2097
2121 try:
2098 try:
2122 code = self.compile(source,filename,symbol)
2099 code = self.compile(source,filename,symbol)
2123 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2100 except (OverflowError, SyntaxError, ValueError, TypeError, MemoryError):
2124 # Case 1
2101 # Case 1
2125 self.showsyntaxerror(filename)
2102 self.showsyntaxerror(filename)
2126 return None
2103 return None
2127
2104
2128 if code is None:
2105 if code is None:
2129 # Case 2
2106 # Case 2
2130 return True
2107 return True
2131
2108
2132 # Case 3
2109 # Case 3
2133 # We store the code object so that threaded shells and
2110 # We store the code object so that threaded shells and
2134 # custom exception handlers can access all this info if needed.
2111 # custom exception handlers can access all this info if needed.
2135 # The source corresponding to this can be obtained from the
2112 # The source corresponding to this can be obtained from the
2136 # buffer attribute as '\n'.join(self.buffer).
2113 # buffer attribute as '\n'.join(self.buffer).
2137 self.code_to_run = code
2114 self.code_to_run = code
2138 # now actually execute the code object
2115 # now actually execute the code object
2139 if self.runcode(code) == 0:
2116 if self.runcode(code) == 0:
2140 return False
2117 return False
2141 else:
2118 else:
2142 return None
2119 return None
2143
2120
2144 def runcode(self,code_obj):
2121 def runcode(self,code_obj):
2145 """Execute a code object.
2122 """Execute a code object.
2146
2123
2147 When an exception occurs, self.showtraceback() is called to display a
2124 When an exception occurs, self.showtraceback() is called to display a
2148 traceback.
2125 traceback.
2149
2126
2150 Return value: a flag indicating whether the code to be run completed
2127 Return value: a flag indicating whether the code to be run completed
2151 successfully:
2128 successfully:
2152
2129
2153 - 0: successful execution.
2130 - 0: successful execution.
2154 - 1: an error occurred.
2131 - 1: an error occurred.
2155 """
2132 """
2156
2133
2157 # Set our own excepthook in case the user code tries to call it
2134 # Set our own excepthook in case the user code tries to call it
2158 # directly, so that the IPython crash handler doesn't get triggered
2135 # directly, so that the IPython crash handler doesn't get triggered
2159 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2136 old_excepthook,sys.excepthook = sys.excepthook, self.excepthook
2160
2137
2161 # we save the original sys.excepthook in the instance, in case config
2138 # we save the original sys.excepthook in the instance, in case config
2162 # code (such as magics) needs access to it.
2139 # code (such as magics) needs access to it.
2163 self.sys_excepthook = old_excepthook
2140 self.sys_excepthook = old_excepthook
2164 outflag = 1 # happens in more places, so it's easier as default
2141 outflag = 1 # happens in more places, so it's easier as default
2165 try:
2142 try:
2166 try:
2143 try:
2167 self.hooks.pre_runcode_hook()
2144 self.hooks.pre_runcode_hook()
2168 exec code_obj in self.user_global_ns, self.user_ns
2145 exec code_obj in self.user_global_ns, self.user_ns
2169 finally:
2146 finally:
2170 # Reset our crash handler in place
2147 # Reset our crash handler in place
2171 sys.excepthook = old_excepthook
2148 sys.excepthook = old_excepthook
2172 except SystemExit:
2149 except SystemExit:
2173 self.resetbuffer()
2150 self.resetbuffer()
2174 self.showtraceback()
2151 self.showtraceback()
2175 warn("Type %exit or %quit to exit IPython "
2152 warn("Type %exit or %quit to exit IPython "
2176 "(%Exit or %Quit do so unconditionally).",level=1)
2153 "(%Exit or %Quit do so unconditionally).",level=1)
2177 except self.custom_exceptions:
2154 except self.custom_exceptions:
2178 etype,value,tb = sys.exc_info()
2155 etype,value,tb = sys.exc_info()
2179 self.CustomTB(etype,value,tb)
2156 self.CustomTB(etype,value,tb)
2180 except:
2157 except:
2181 self.showtraceback()
2158 self.showtraceback()
2182 else:
2159 else:
2183 outflag = 0
2160 outflag = 0
2184 if softspace(sys.stdout, 0):
2161 if softspace(sys.stdout, 0):
2185 print
2162 print
2186 # Flush out code object which has been run (and source)
2163 # Flush out code object which has been run (and source)
2187 self.code_to_run = None
2164 self.code_to_run = None
2188 return outflag
2165 return outflag
2189
2166
2190 def push_line(self, line):
2167 def push_line(self, line):
2191 """Push a line to the interpreter.
2168 """Push a line to the interpreter.
2192
2169
2193 The line should not have a trailing newline; it may have
2170 The line should not have a trailing newline; it may have
2194 internal newlines. The line is appended to a buffer and the
2171 internal newlines. The line is appended to a buffer and the
2195 interpreter's runsource() method is called with the
2172 interpreter's runsource() method is called with the
2196 concatenated contents of the buffer as source. If this
2173 concatenated contents of the buffer as source. If this
2197 indicates that the command was executed or invalid, the buffer
2174 indicates that the command was executed or invalid, the buffer
2198 is reset; otherwise, the command is incomplete, and the buffer
2175 is reset; otherwise, the command is incomplete, and the buffer
2199 is left as it was after the line was appended. The return
2176 is left as it was after the line was appended. The return
2200 value is 1 if more input is required, 0 if the line was dealt
2177 value is 1 if more input is required, 0 if the line was dealt
2201 with in some way (this is the same as runsource()).
2178 with in some way (this is the same as runsource()).
2202 """
2179 """
2203
2180
2204 # autoindent management should be done here, and not in the
2181 # autoindent management should be done here, and not in the
2205 # interactive loop, since that one is only seen by keyboard input. We
2182 # interactive loop, since that one is only seen by keyboard input. We
2206 # need this done correctly even for code run via runlines (which uses
2183 # need this done correctly even for code run via runlines (which uses
2207 # push).
2184 # push).
2208
2185
2209 #print 'push line: <%s>' % line # dbg
2186 #print 'push line: <%s>' % line # dbg
2210 for subline in line.splitlines():
2187 for subline in line.splitlines():
2211 self._autoindent_update(subline)
2188 self._autoindent_update(subline)
2212 self.buffer.append(line)
2189 self.buffer.append(line)
2213 more = self.runsource('\n'.join(self.buffer), self.filename)
2190 more = self.runsource('\n'.join(self.buffer), self.filename)
2214 if not more:
2191 if not more:
2215 self.resetbuffer()
2192 self.resetbuffer()
2216 return more
2193 return more
2217
2194
2218 def _autoindent_update(self,line):
2195 def _autoindent_update(self,line):
2219 """Keep track of the indent level."""
2196 """Keep track of the indent level."""
2220
2197
2221 #debugx('line')
2198 #debugx('line')
2222 #debugx('self.indent_current_nsp')
2199 #debugx('self.indent_current_nsp')
2223 if self.autoindent:
2200 if self.autoindent:
2224 if line:
2201 if line:
2225 inisp = num_ini_spaces(line)
2202 inisp = num_ini_spaces(line)
2226 if inisp < self.indent_current_nsp:
2203 if inisp < self.indent_current_nsp:
2227 self.indent_current_nsp = inisp
2204 self.indent_current_nsp = inisp
2228
2205
2229 if line[-1] == ':':
2206 if line[-1] == ':':
2230 self.indent_current_nsp += 4
2207 self.indent_current_nsp += 4
2231 elif dedent_re.match(line):
2208 elif dedent_re.match(line):
2232 self.indent_current_nsp -= 4
2209 self.indent_current_nsp -= 4
2233 else:
2210 else:
2234 self.indent_current_nsp = 0
2211 self.indent_current_nsp = 0
2235
2212
2236 def resetbuffer(self):
2213 def resetbuffer(self):
2237 """Reset the input buffer."""
2214 """Reset the input buffer."""
2238 self.buffer[:] = []
2215 self.buffer[:] = []
2239
2216
2240 def raw_input(self,prompt='',continue_prompt=False):
2217 def raw_input(self,prompt='',continue_prompt=False):
2241 """Write a prompt and read a line.
2218 """Write a prompt and read a line.
2242
2219
2243 The returned line does not include the trailing newline.
2220 The returned line does not include the trailing newline.
2244 When the user enters the EOF key sequence, EOFError is raised.
2221 When the user enters the EOF key sequence, EOFError is raised.
2245
2222
2246 Optional inputs:
2223 Optional inputs:
2247
2224
2248 - prompt(''): a string to be printed to prompt the user.
2225 - prompt(''): a string to be printed to prompt the user.
2249
2226
2250 - continue_prompt(False): whether this line is the first one or a
2227 - continue_prompt(False): whether this line is the first one or a
2251 continuation in a sequence of inputs.
2228 continuation in a sequence of inputs.
2252 """
2229 """
2253 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2230 # growl.notify("raw_input: ", "prompt = %r\ncontinue_prompt = %s" % (prompt, continue_prompt))
2254
2231
2255 # Code run by the user may have modified the readline completer state.
2232 # Code run by the user may have modified the readline completer state.
2256 # We must ensure that our completer is back in place.
2233 # We must ensure that our completer is back in place.
2257
2234
2258 if self.has_readline:
2235 if self.has_readline:
2259 self.set_completer()
2236 self.set_completer()
2260
2237
2261 try:
2238 try:
2262 line = raw_input_original(prompt).decode(self.stdin_encoding)
2239 line = raw_input_original(prompt).decode(self.stdin_encoding)
2263 except ValueError:
2240 except ValueError:
2264 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2241 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
2265 " or sys.stdout.close()!\nExiting IPython!")
2242 " or sys.stdout.close()!\nExiting IPython!")
2266 self.ask_exit()
2243 self.ask_exit()
2267 return ""
2244 return ""
2268
2245
2269 # Try to be reasonably smart about not re-indenting pasted input more
2246 # Try to be reasonably smart about not re-indenting pasted input more
2270 # than necessary. We do this by trimming out the auto-indent initial
2247 # than necessary. We do this by trimming out the auto-indent initial
2271 # spaces, if the user's actual input started itself with whitespace.
2248 # spaces, if the user's actual input started itself with whitespace.
2272 #debugx('self.buffer[-1]')
2249 #debugx('self.buffer[-1]')
2273
2250
2274 if self.autoindent:
2251 if self.autoindent:
2275 if num_ini_spaces(line) > self.indent_current_nsp:
2252 if num_ini_spaces(line) > self.indent_current_nsp:
2276 line = line[self.indent_current_nsp:]
2253 line = line[self.indent_current_nsp:]
2277 self.indent_current_nsp = 0
2254 self.indent_current_nsp = 0
2278
2255
2279 # store the unfiltered input before the user has any chance to modify
2256 # store the unfiltered input before the user has any chance to modify
2280 # it.
2257 # it.
2281 if line.strip():
2258 if line.strip():
2282 if continue_prompt:
2259 if continue_prompt:
2283 self.input_hist_raw[-1] += '%s\n' % line
2260 self.input_hist_raw[-1] += '%s\n' % line
2284 if self.has_readline and self.readline_use:
2261 if self.has_readline and self.readline_use:
2285 try:
2262 try:
2286 histlen = self.readline.get_current_history_length()
2263 histlen = self.readline.get_current_history_length()
2287 if histlen > 1:
2264 if histlen > 1:
2288 newhist = self.input_hist_raw[-1].rstrip()
2265 newhist = self.input_hist_raw[-1].rstrip()
2289 self.readline.remove_history_item(histlen-1)
2266 self.readline.remove_history_item(histlen-1)
2290 self.readline.replace_history_item(histlen-2,
2267 self.readline.replace_history_item(histlen-2,
2291 newhist.encode(self.stdin_encoding))
2268 newhist.encode(self.stdin_encoding))
2292 except AttributeError:
2269 except AttributeError:
2293 pass # re{move,place}_history_item are new in 2.4.
2270 pass # re{move,place}_history_item are new in 2.4.
2294 else:
2271 else:
2295 self.input_hist_raw.append('%s\n' % line)
2272 self.input_hist_raw.append('%s\n' % line)
2296 # only entries starting at first column go to shadow history
2273 # only entries starting at first column go to shadow history
2297 if line.lstrip() == line:
2274 if line.lstrip() == line:
2298 self.shadowhist.add(line.strip())
2275 self.shadowhist.add(line.strip())
2299 elif not continue_prompt:
2276 elif not continue_prompt:
2300 self.input_hist_raw.append('\n')
2277 self.input_hist_raw.append('\n')
2301 try:
2278 try:
2302 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2279 lineout = self.prefilter_manager.prefilter_lines(line,continue_prompt)
2303 except:
2280 except:
2304 # blanket except, in case a user-defined prefilter crashes, so it
2281 # blanket except, in case a user-defined prefilter crashes, so it
2305 # can't take all of ipython with it.
2282 # can't take all of ipython with it.
2306 self.showtraceback()
2283 self.showtraceback()
2307 return ''
2284 return ''
2308 else:
2285 else:
2309 return lineout
2286 return lineout
2310
2287
2311 #-------------------------------------------------------------------------
2288 #-------------------------------------------------------------------------
2312 # Working with components
2289 # Working with components
2313 #-------------------------------------------------------------------------
2290 #-------------------------------------------------------------------------
2314
2291
2315 def get_component(self, name=None, klass=None):
2292 def get_component(self, name=None, klass=None):
2316 """Fetch a component by name and klass in my tree."""
2293 """Fetch a component by name and klass in my tree."""
2317 c = Component.get_instances(root=self, name=name, klass=klass)
2294 c = Component.get_instances(root=self, name=name, klass=klass)
2318 if len(c) == 0:
2295 if len(c) == 0:
2319 return None
2296 return None
2320 if len(c) == 1:
2297 if len(c) == 1:
2321 return c[0]
2298 return c[0]
2322 else:
2299 else:
2323 return c
2300 return c
2324
2301
2325 #-------------------------------------------------------------------------
2302 #-------------------------------------------------------------------------
2326 # IPython extensions
2303 # IPython extensions
2327 #-------------------------------------------------------------------------
2304 #-------------------------------------------------------------------------
2328
2305
2329 def load_extension(self, module_str):
2306 def load_extension(self, module_str):
2330 """Load an IPython extension by its module name.
2307 """Load an IPython extension by its module name.
2331
2308
2332 An IPython extension is an importable Python module that has
2309 An IPython extension is an importable Python module that has
2333 a function with the signature::
2310 a function with the signature::
2334
2311
2335 def load_ipython_extension(ipython):
2312 def load_ipython_extension(ipython):
2336 # Do things with ipython
2313 # Do things with ipython
2337
2314
2338 This function is called after your extension is imported and the
2315 This function is called after your extension is imported and the
2339 currently active :class:`InteractiveShell` instance is passed as
2316 currently active :class:`InteractiveShell` instance is passed as
2340 the only argument. You can do anything you want with IPython at
2317 the only argument. You can do anything you want with IPython at
2341 that point, including defining new magic and aliases, adding new
2318 that point, including defining new magic and aliases, adding new
2342 components, etc.
2319 components, etc.
2343
2320
2344 The :func:`load_ipython_extension` will be called again is you
2321 The :func:`load_ipython_extension` will be called again is you
2345 load or reload the extension again. It is up to the extension
2322 load or reload the extension again. It is up to the extension
2346 author to add code to manage that.
2323 author to add code to manage that.
2347
2324
2348 You can put your extension modules anywhere you want, as long as
2325 You can put your extension modules anywhere you want, as long as
2349 they can be imported by Python's standard import mechanism. However,
2326 they can be imported by Python's standard import mechanism. However,
2350 to make it easy to write extensions, you can also put your extensions
2327 to make it easy to write extensions, you can also put your extensions
2351 in ``os.path.join(self.ipython_dir, 'extensions')``. This directory
2328 in ``os.path.join(self.ipython_dir, 'extensions')``. This directory
2352 is added to ``sys.path`` automatically.
2329 is added to ``sys.path`` automatically.
2353 """
2330 """
2354 from IPython.utils.syspathcontext import prepended_to_syspath
2331 from IPython.utils.syspathcontext import prepended_to_syspath
2355
2332
2356 if module_str not in sys.modules:
2333 if module_str not in sys.modules:
2357 with prepended_to_syspath(self.ipython_extension_dir):
2334 with prepended_to_syspath(self.ipython_extension_dir):
2358 __import__(module_str)
2335 __import__(module_str)
2359 mod = sys.modules[module_str]
2336 mod = sys.modules[module_str]
2360 self._call_load_ipython_extension(mod)
2337 self._call_load_ipython_extension(mod)
2361
2338
2362 def unload_extension(self, module_str):
2339 def unload_extension(self, module_str):
2363 """Unload an IPython extension by its module name.
2340 """Unload an IPython extension by its module name.
2364
2341
2365 This function looks up the extension's name in ``sys.modules`` and
2342 This function looks up the extension's name in ``sys.modules`` and
2366 simply calls ``mod.unload_ipython_extension(self)``.
2343 simply calls ``mod.unload_ipython_extension(self)``.
2367 """
2344 """
2368 if module_str in sys.modules:
2345 if module_str in sys.modules:
2369 mod = sys.modules[module_str]
2346 mod = sys.modules[module_str]
2370 self._call_unload_ipython_extension(mod)
2347 self._call_unload_ipython_extension(mod)
2371
2348
2372 def reload_extension(self, module_str):
2349 def reload_extension(self, module_str):
2373 """Reload an IPython extension by calling reload.
2350 """Reload an IPython extension by calling reload.
2374
2351
2375 If the module has not been loaded before,
2352 If the module has not been loaded before,
2376 :meth:`InteractiveShell.load_extension` is called. Otherwise
2353 :meth:`InteractiveShell.load_extension` is called. Otherwise
2377 :func:`reload` is called and then the :func:`load_ipython_extension`
2354 :func:`reload` is called and then the :func:`load_ipython_extension`
2378 function of the module, if it exists is called.
2355 function of the module, if it exists is called.
2379 """
2356 """
2380 from IPython.utils.syspathcontext import prepended_to_syspath
2357 from IPython.utils.syspathcontext import prepended_to_syspath
2381
2358
2382 with prepended_to_syspath(self.ipython_extension_dir):
2359 with prepended_to_syspath(self.ipython_extension_dir):
2383 if module_str in sys.modules:
2360 if module_str in sys.modules:
2384 mod = sys.modules[module_str]
2361 mod = sys.modules[module_str]
2385 reload(mod)
2362 reload(mod)
2386 self._call_load_ipython_extension(mod)
2363 self._call_load_ipython_extension(mod)
2387 else:
2364 else:
2388 self.load_extension(module_str)
2365 self.load_extension(module_str)
2389
2366
2390 def _call_load_ipython_extension(self, mod):
2367 def _call_load_ipython_extension(self, mod):
2391 if hasattr(mod, 'load_ipython_extension'):
2368 if hasattr(mod, 'load_ipython_extension'):
2392 mod.load_ipython_extension(self)
2369 mod.load_ipython_extension(self)
2393
2370
2394 def _call_unload_ipython_extension(self, mod):
2371 def _call_unload_ipython_extension(self, mod):
2395 if hasattr(mod, 'unload_ipython_extension'):
2372 if hasattr(mod, 'unload_ipython_extension'):
2396 mod.unload_ipython_extension(self)
2373 mod.unload_ipython_extension(self)
2397
2374
2398 #-------------------------------------------------------------------------
2375 #-------------------------------------------------------------------------
2399 # Things related to the prefilter
2376 # Things related to the prefilter
2400 #-------------------------------------------------------------------------
2377 #-------------------------------------------------------------------------
2401
2378
2402 def init_prefilter(self):
2379 def init_prefilter(self):
2403 self.prefilter_manager = PrefilterManager(self, config=self.config)
2380 self.prefilter_manager = PrefilterManager(self, config=self.config)
2404
2381
2405 #-------------------------------------------------------------------------
2382 #-------------------------------------------------------------------------
2406 # Utilities
2383 # Utilities
2407 #-------------------------------------------------------------------------
2384 #-------------------------------------------------------------------------
2408
2385
2409 def getoutput(self, cmd):
2386 def getoutput(self, cmd):
2410 return getoutput(self.var_expand(cmd,depth=2),
2387 return getoutput(self.var_expand(cmd,depth=2),
2411 header=self.system_header,
2388 header=self.system_header,
2412 verbose=self.system_verbose)
2389 verbose=self.system_verbose)
2413
2390
2414 def getoutputerror(self, cmd):
2391 def getoutputerror(self, cmd):
2415 return getoutputerror(self.var_expand(cmd,depth=2),
2392 return getoutputerror(self.var_expand(cmd,depth=2),
2416 header=self.system_header,
2393 header=self.system_header,
2417 verbose=self.system_verbose)
2394 verbose=self.system_verbose)
2418
2395
2419 def var_expand(self,cmd,depth=0):
2396 def var_expand(self,cmd,depth=0):
2420 """Expand python variables in a string.
2397 """Expand python variables in a string.
2421
2398
2422 The depth argument indicates how many frames above the caller should
2399 The depth argument indicates how many frames above the caller should
2423 be walked to look for the local namespace where to expand variables.
2400 be walked to look for the local namespace where to expand variables.
2424
2401
2425 The global namespace for expansion is always the user's interactive
2402 The global namespace for expansion is always the user's interactive
2426 namespace.
2403 namespace.
2427 """
2404 """
2428
2405
2429 return str(ItplNS(cmd,
2406 return str(ItplNS(cmd,
2430 self.user_ns, # globals
2407 self.user_ns, # globals
2431 # Skip our own frame in searching for locals:
2408 # Skip our own frame in searching for locals:
2432 sys._getframe(depth+1).f_locals # locals
2409 sys._getframe(depth+1).f_locals # locals
2433 ))
2410 ))
2434
2411
2435 def mktempfile(self,data=None):
2412 def mktempfile(self,data=None):
2436 """Make a new tempfile and return its filename.
2413 """Make a new tempfile and return its filename.
2437
2414
2438 This makes a call to tempfile.mktemp, but it registers the created
2415 This makes a call to tempfile.mktemp, but it registers the created
2439 filename internally so ipython cleans it up at exit time.
2416 filename internally so ipython cleans it up at exit time.
2440
2417
2441 Optional inputs:
2418 Optional inputs:
2442
2419
2443 - data(None): if data is given, it gets written out to the temp file
2420 - data(None): if data is given, it gets written out to the temp file
2444 immediately, and the file is closed again."""
2421 immediately, and the file is closed again."""
2445
2422
2446 filename = tempfile.mktemp('.py','ipython_edit_')
2423 filename = tempfile.mktemp('.py','ipython_edit_')
2447 self.tempfiles.append(filename)
2424 self.tempfiles.append(filename)
2448
2425
2449 if data:
2426 if data:
2450 tmp_file = open(filename,'w')
2427 tmp_file = open(filename,'w')
2451 tmp_file.write(data)
2428 tmp_file.write(data)
2452 tmp_file.close()
2429 tmp_file.close()
2453 return filename
2430 return filename
2454
2431
2455 def write(self,data):
2432 def write(self,data):
2456 """Write a string to the default output"""
2433 """Write a string to the default output"""
2457 Term.cout.write(data)
2434 Term.cout.write(data)
2458
2435
2459 def write_err(self,data):
2436 def write_err(self,data):
2460 """Write a string to the default error output"""
2437 """Write a string to the default error output"""
2461 Term.cerr.write(data)
2438 Term.cerr.write(data)
2462
2439
2463 def ask_yes_no(self,prompt,default=True):
2440 def ask_yes_no(self,prompt,default=True):
2464 if self.quiet:
2441 if self.quiet:
2465 return True
2442 return True
2466 return ask_yes_no(prompt,default)
2443 return ask_yes_no(prompt,default)
2467
2444
2468 #-------------------------------------------------------------------------
2445 #-------------------------------------------------------------------------
2469 # Things related to GUI support and pylab
2446 # Things related to GUI support and pylab
2470 #-------------------------------------------------------------------------
2447 #-------------------------------------------------------------------------
2471
2448
2472 def enable_pylab(self, gui=None):
2449 def enable_pylab(self, gui=None):
2473 """Activate pylab support at runtime.
2450 """Activate pylab support at runtime.
2474
2451
2475 This turns on support for matplotlib, preloads into the interactive
2452 This turns on support for matplotlib, preloads into the interactive
2476 namespace all of numpy and pylab, and configures IPython to correcdtly
2453 namespace all of numpy and pylab, and configures IPython to correcdtly
2477 interact with the GUI event loop. The GUI backend to be used can be
2454 interact with the GUI event loop. The GUI backend to be used can be
2478 optionally selected with the optional :param:`gui` argument.
2455 optionally selected with the optional :param:`gui` argument.
2479
2456
2480 Parameters
2457 Parameters
2481 ----------
2458 ----------
2482 gui : optional, string
2459 gui : optional, string
2483
2460
2484 If given, dictates the choice of matplotlib GUI backend to use
2461 If given, dictates the choice of matplotlib GUI backend to use
2485 (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or
2462 (should be one of IPython's supported backends, 'tk', 'qt', 'wx' or
2486 'gtk'), otherwise we use the default chosen by matplotlib (as
2463 'gtk'), otherwise we use the default chosen by matplotlib (as
2487 dictated by the matplotlib build-time options plus the user's
2464 dictated by the matplotlib build-time options plus the user's
2488 matplotlibrc configuration file).
2465 matplotlibrc configuration file).
2489 """
2466 """
2490 # We want to prevent the loading of pylab to pollute the user's
2467 # We want to prevent the loading of pylab to pollute the user's
2491 # namespace as shown by the %who* magics, so we execute the activation
2468 # namespace as shown by the %who* magics, so we execute the activation
2492 # code in an empty namespace, and we update *both* user_ns and
2469 # code in an empty namespace, and we update *both* user_ns and
2493 # user_config_ns with this information.
2470 # user_config_ns with this information.
2494 ns = {}
2471 ns = {}
2495 gui = pylab_activate(ns, gui)
2472 gui = pylab_activate(ns, gui)
2496 self.user_ns.update(ns)
2473 self.user_ns.update(ns)
2497 self.user_config_ns.update(ns)
2474 self.user_config_ns.update(ns)
2498 # Now we must activate the gui pylab wants to use, and fix %run to take
2475 # Now we must activate the gui pylab wants to use, and fix %run to take
2499 # plot updates into account
2476 # plot updates into account
2500 enable_gui(gui)
2477 enable_gui(gui)
2501 self.magic_run = self._pylab_magic_run
2478 self.magic_run = self._pylab_magic_run
2502
2479
2503 #-------------------------------------------------------------------------
2480 #-------------------------------------------------------------------------
2504 # Things related to IPython exiting
2481 # Things related to IPython exiting
2505 #-------------------------------------------------------------------------
2482 #-------------------------------------------------------------------------
2506
2483
2507 def ask_exit(self):
2484 def ask_exit(self):
2508 """ Ask the shell to exit. Can be overiden and used as a callback. """
2485 """ Ask the shell to exit. Can be overiden and used as a callback. """
2509 self.exit_now = True
2486 self.exit_now = True
2510
2487
2511 def exit(self):
2488 def exit(self):
2512 """Handle interactive exit.
2489 """Handle interactive exit.
2513
2490
2514 This method calls the ask_exit callback."""
2491 This method calls the ask_exit callback."""
2515 if self.confirm_exit:
2492 if self.confirm_exit:
2516 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2493 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
2517 self.ask_exit()
2494 self.ask_exit()
2518 else:
2495 else:
2519 self.ask_exit()
2496 self.ask_exit()
2520
2497
2521 def atexit_operations(self):
2498 def atexit_operations(self):
2522 """This will be executed at the time of exit.
2499 """This will be executed at the time of exit.
2523
2500
2524 Saving of persistent data should be performed here.
2501 Saving of persistent data should be performed here.
2525 """
2502 """
2526 self.savehist()
2503 self.savehist()
2527
2504
2528 # Cleanup all tempfiles left around
2505 # Cleanup all tempfiles left around
2529 for tfile in self.tempfiles:
2506 for tfile in self.tempfiles:
2530 try:
2507 try:
2531 os.unlink(tfile)
2508 os.unlink(tfile)
2532 except OSError:
2509 except OSError:
2533 pass
2510 pass
2534
2511
2535 # Clear all user namespaces to release all references cleanly.
2512 # Clear all user namespaces to release all references cleanly.
2536 self.reset()
2513 self.reset()
2537
2514
2538 # Run user hooks
2515 # Run user hooks
2539 self.hooks.shutdown_hook()
2516 self.hooks.shutdown_hook()
2540
2517
2541 def cleanup(self):
2518 def cleanup(self):
2542 self.restore_sys_module_state()
2519 self.restore_sys_module_state()
2543
2520
2544
2521
@@ -1,270 +1,264 b''
1 """
1 """
2 Frontend class that uses IPython0 to prefilter the inputs.
2 Frontend class that uses IPython0 to prefilter the inputs.
3
3
4 Using the IPython0 mechanism gives us access to the magics.
4 Using the IPython0 mechanism gives us access to the magics.
5
5
6 This is a transitory class, used here to do the transition between
6 This is a transitory class, used here to do the transition between
7 ipython0 and ipython1. This class is meant to be short-lived as more
7 ipython0 and ipython1. This class is meant to be short-lived as more
8 functionnality is abstracted out of ipython0 in reusable functions and
8 functionnality is abstracted out of ipython0 in reusable functions and
9 is added on the interpreter. This class can be a used to guide this
9 is added on the interpreter. This class can be a used to guide this
10 refactoring.
10 refactoring.
11 """
11 """
12
12
13 #-------------------------------------------------------------------------------
13 #-------------------------------------------------------------------------------
14 # Copyright (C) 2008 The IPython Development Team
14 # Copyright (C) 2008 The IPython Development Team
15 #
15 #
16 # Distributed under the terms of the BSD License. The full license is in
16 # Distributed under the terms of the BSD License. The full license is in
17 # the file COPYING, distributed as part of this software.
17 # the file COPYING, distributed as part of this software.
18 #-------------------------------------------------------------------------------
18 #-------------------------------------------------------------------------------
19
19
20 #-------------------------------------------------------------------------------
20 #-------------------------------------------------------------------------------
21 # Imports
21 # Imports
22 #-------------------------------------------------------------------------------
22 #-------------------------------------------------------------------------------
23 import sys
23 import sys
24 import pydoc
24 import pydoc
25 import os
25 import os
26 import re
26 import re
27 import __builtin__
27 import __builtin__
28
28
29 from IPython.core.ipapp import IPythonApp
29 from IPython.core.ipapp import IPythonApp
30 from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap
30 from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap
31
31
32 from IPython.kernel.core.sync_traceback_trap import SyncTracebackTrap
32 from IPython.kernel.core.sync_traceback_trap import SyncTracebackTrap
33
33
34 from IPython.utils.genutils import Term
34 from IPython.utils.genutils import Term
35
35
36 from linefrontendbase import LineFrontEndBase, common_prefix
36 from linefrontendbase import LineFrontEndBase, common_prefix
37
37
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39 # Utility functions
39 # Utility functions
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41
41
42 def mk_system_call(system_call_function, command):
42 def mk_system_call(system_call_function, command):
43 """ given a os.system replacement, and a leading string command,
43 """ given a os.system replacement, and a leading string command,
44 returns a function that will execute the command with the given
44 returns a function that will execute the command with the given
45 argument string.
45 argument string.
46 """
46 """
47 def my_system_call(args):
47 def my_system_call(args):
48 system_call_function("%s %s" % (command, args))
48 system_call_function("%s %s" % (command, args))
49
49
50 my_system_call.__doc__ = "Calls %s" % command
50 my_system_call.__doc__ = "Calls %s" % command
51 return my_system_call
51 return my_system_call
52
52
53 #-------------------------------------------------------------------------------
53 #-------------------------------------------------------------------------------
54 # Frontend class using ipython0 to do the prefiltering.
54 # Frontend class using ipython0 to do the prefiltering.
55 #-------------------------------------------------------------------------------
55 #-------------------------------------------------------------------------------
56 class PrefilterFrontEnd(LineFrontEndBase):
56 class PrefilterFrontEnd(LineFrontEndBase):
57 """ Class that uses ipython0 to do prefilter the input, do the
57 """ Class that uses ipython0 to do prefilter the input, do the
58 completion and the magics.
58 completion and the magics.
59
59
60 The core trick is to use an ipython0 instance to prefilter the
60 The core trick is to use an ipython0 instance to prefilter the
61 input, and share the namespace between the interpreter instance used
61 input, and share the namespace between the interpreter instance used
62 to execute the statements and the ipython0 used for code
62 to execute the statements and the ipython0 used for code
63 completion...
63 completion...
64 """
64 """
65
65
66 debug = False
66 debug = False
67
67
68 def __init__(self, ipython0=None, argv=None, *args, **kwargs):
68 def __init__(self, ipython0=None, argv=None, *args, **kwargs):
69 """ Parameters
69 """ Parameters
70 ----------
70 ----------
71
71
72 ipython0: an optional ipython0 instance to use for command
72 ipython0: an optional ipython0 instance to use for command
73 prefiltering and completion.
73 prefiltering and completion.
74
74
75 argv : list, optional
75 argv : list, optional
76 Used as the instance's argv value. If not given, [] is used.
76 Used as the instance's argv value. If not given, [] is used.
77 """
77 """
78 if argv is None:
78 if argv is None:
79 argv = ['--no-banner']
79 argv = ['--no-banner']
80 # This is a hack to avoid the IPython exception hook to trigger
81 # on exceptions (https://bugs.launchpad.net/bugs/337105)
82 # XXX: This is horrible: module-leve monkey patching -> side
83 # effects.
84 from IPython.core import iplib
85 iplib.InteractiveShell.isthreaded = True
86
80
87 LineFrontEndBase.__init__(self, *args, **kwargs)
81 LineFrontEndBase.__init__(self, *args, **kwargs)
88 self.shell.output_trap = RedirectorOutputTrap(
82 self.shell.output_trap = RedirectorOutputTrap(
89 out_callback=self.write,
83 out_callback=self.write,
90 err_callback=self.write,
84 err_callback=self.write,
91 )
85 )
92 self.shell.traceback_trap = SyncTracebackTrap(
86 self.shell.traceback_trap = SyncTracebackTrap(
93 formatters=self.shell.traceback_trap.formatters,
87 formatters=self.shell.traceback_trap.formatters,
94 )
88 )
95
89
96 # Start the ipython0 instance:
90 # Start the ipython0 instance:
97 self.save_output_hooks()
91 self.save_output_hooks()
98 if ipython0 is None:
92 if ipython0 is None:
99 # Instanciate an IPython0 interpreter to be able to use the
93 # Instanciate an IPython0 interpreter to be able to use the
100 # prefiltering.
94 # prefiltering.
101 # Suppress all key input, to avoid waiting
95 # Suppress all key input, to avoid waiting
102 def my_rawinput(x=None):
96 def my_rawinput(x=None):
103 return '\n'
97 return '\n'
104 old_rawinput = __builtin__.raw_input
98 old_rawinput = __builtin__.raw_input
105 __builtin__.raw_input = my_rawinput
99 __builtin__.raw_input = my_rawinput
106 ipython0 = IPythonApp(argv=argv,
100 ipython0 = IPythonApp(argv=argv,
107 user_ns=self.shell.user_ns,
101 user_ns=self.shell.user_ns,
108 user_global_ns=self.shell.user_global_ns)
102 user_global_ns=self.shell.user_global_ns)
109 ipython0.initialize()
103 ipython0.initialize()
110 __builtin__.raw_input = old_rawinput
104 __builtin__.raw_input = old_rawinput
111 # XXX This will need to be updated as we refactor things, but for now,
105 # XXX This will need to be updated as we refactor things, but for now,
112 # the .shell attribute of the ipythonapp instance conforms to the old
106 # the .shell attribute of the ipythonapp instance conforms to the old
113 # api.
107 # api.
114 self.ipython0 = ipython0.shell
108 self.ipython0 = ipython0.shell
115 # Set the pager:
109 # Set the pager:
116 self.ipython0.set_hook('show_in_pager',
110 self.ipython0.set_hook('show_in_pager',
117 lambda s, string: self.write("\n" + string))
111 lambda s, string: self.write("\n" + string))
118 self.ipython0.write = self.write
112 self.ipython0.write = self.write
119 self._ip = _ip = self.ipython0
113 self._ip = _ip = self.ipython0
120 # Make sure the raw system call doesn't get called, as we don't
114 # Make sure the raw system call doesn't get called, as we don't
121 # have a stdin accessible.
115 # have a stdin accessible.
122 self._ip.system = self.system_call
116 self._ip.system = self.system_call
123 # XXX: Muck around with magics so that they work better
117 # XXX: Muck around with magics so that they work better
124 # in our environment
118 # in our environment
125 if not sys.platform.startswith('win'):
119 if not sys.platform.startswith('win'):
126 self.ipython0.magic_ls = mk_system_call(self.system_call,
120 self.ipython0.magic_ls = mk_system_call(self.system_call,
127 'ls -CF')
121 'ls -CF')
128 # And now clean up the mess created by ipython0
122 # And now clean up the mess created by ipython0
129 self.release_output()
123 self.release_output()
130
124
131
125
132 if not 'banner' in kwargs and self.banner is None:
126 if not 'banner' in kwargs and self.banner is None:
133 self.banner = self.ipython0.banner
127 self.banner = self.ipython0.banner
134
128
135 # FIXME: __init__ and start should be two different steps
129 # FIXME: __init__ and start should be two different steps
136 self.start()
130 self.start()
137
131
138 #--------------------------------------------------------------------------
132 #--------------------------------------------------------------------------
139 # FrontEndBase interface
133 # FrontEndBase interface
140 #--------------------------------------------------------------------------
134 #--------------------------------------------------------------------------
141
135
142 def show_traceback(self):
136 def show_traceback(self):
143 """ Use ipython0 to capture the last traceback and display it.
137 """ Use ipython0 to capture the last traceback and display it.
144 """
138 """
145 # Don't do the capture; the except_hook has already done some
139 # Don't do the capture; the except_hook has already done some
146 # modifications to the IO streams, if we store them, we'll be
140 # modifications to the IO streams, if we store them, we'll be
147 # storing the wrong ones.
141 # storing the wrong ones.
148 #self.capture_output()
142 #self.capture_output()
149 self.ipython0.showtraceback(tb_offset=-1)
143 self.ipython0.showtraceback(tb_offset=-1)
150 self.release_output()
144 self.release_output()
151
145
152
146
153 def execute(self, python_string, raw_string=None):
147 def execute(self, python_string, raw_string=None):
154 if self.debug:
148 if self.debug:
155 print 'Executing Python code:', repr(python_string)
149 print 'Executing Python code:', repr(python_string)
156 self.capture_output()
150 self.capture_output()
157 LineFrontEndBase.execute(self, python_string,
151 LineFrontEndBase.execute(self, python_string,
158 raw_string=raw_string)
152 raw_string=raw_string)
159 self.release_output()
153 self.release_output()
160
154
161
155
162 def save_output_hooks(self):
156 def save_output_hooks(self):
163 """ Store all the output hooks we can think of, to be able to
157 """ Store all the output hooks we can think of, to be able to
164 restore them.
158 restore them.
165
159
166 We need to do this early, as starting the ipython0 instance will
160 We need to do this early, as starting the ipython0 instance will
167 screw ouput hooks.
161 screw ouput hooks.
168 """
162 """
169 self.__old_cout_write = Term.cout.write
163 self.__old_cout_write = Term.cout.write
170 self.__old_cerr_write = Term.cerr.write
164 self.__old_cerr_write = Term.cerr.write
171 self.__old_stdout = sys.stdout
165 self.__old_stdout = sys.stdout
172 self.__old_stderr= sys.stderr
166 self.__old_stderr= sys.stderr
173 self.__old_help_output = pydoc.help.output
167 self.__old_help_output = pydoc.help.output
174 self.__old_display_hook = sys.displayhook
168 self.__old_display_hook = sys.displayhook
175
169
176
170
177 def capture_output(self):
171 def capture_output(self):
178 """ Capture all the output mechanisms we can think of.
172 """ Capture all the output mechanisms we can think of.
179 """
173 """
180 self.save_output_hooks()
174 self.save_output_hooks()
181 Term.cout.write = self.write
175 Term.cout.write = self.write
182 Term.cerr.write = self.write
176 Term.cerr.write = self.write
183 sys.stdout = Term.cout
177 sys.stdout = Term.cout
184 sys.stderr = Term.cerr
178 sys.stderr = Term.cerr
185 pydoc.help.output = self.shell.output_trap.out
179 pydoc.help.output = self.shell.output_trap.out
186
180
187
181
188 def release_output(self):
182 def release_output(self):
189 """ Release all the different captures we have made.
183 """ Release all the different captures we have made.
190 """
184 """
191 Term.cout.write = self.__old_cout_write
185 Term.cout.write = self.__old_cout_write
192 Term.cerr.write = self.__old_cerr_write
186 Term.cerr.write = self.__old_cerr_write
193 sys.stdout = self.__old_stdout
187 sys.stdout = self.__old_stdout
194 sys.stderr = self.__old_stderr
188 sys.stderr = self.__old_stderr
195 pydoc.help.output = self.__old_help_output
189 pydoc.help.output = self.__old_help_output
196 sys.displayhook = self.__old_display_hook
190 sys.displayhook = self.__old_display_hook
197
191
198
192
199 def complete(self, line):
193 def complete(self, line):
200 # FIXME: This should be factored out in the linefrontendbase
194 # FIXME: This should be factored out in the linefrontendbase
201 # method.
195 # method.
202 word = self._get_completion_text(line)
196 word = self._get_completion_text(line)
203 completions = self.ipython0.complete(word)
197 completions = self.ipython0.complete(word)
204 # FIXME: The proper sort should be done in the complete method.
198 # FIXME: The proper sort should be done in the complete method.
205 key = lambda x: x.replace('_', '')
199 key = lambda x: x.replace('_', '')
206 completions.sort(key=key)
200 completions.sort(key=key)
207 if completions:
201 if completions:
208 prefix = common_prefix(completions)
202 prefix = common_prefix(completions)
209 line = line[:-len(word)] + prefix
203 line = line[:-len(word)] + prefix
210 return line, completions
204 return line, completions
211
205
212 #--------------------------------------------------------------------------
206 #--------------------------------------------------------------------------
213 # LineFrontEndBase interface
207 # LineFrontEndBase interface
214 #--------------------------------------------------------------------------
208 #--------------------------------------------------------------------------
215
209
216 def prefilter_input(self, input_string):
210 def prefilter_input(self, input_string):
217 """ Using IPython0 to prefilter the commands to turn them
211 """ Using IPython0 to prefilter the commands to turn them
218 in executable statements that are valid Python strings.
212 in executable statements that are valid Python strings.
219 """
213 """
220 input_string = LineFrontEndBase.prefilter_input(self, input_string)
214 input_string = LineFrontEndBase.prefilter_input(self, input_string)
221 filtered_lines = []
215 filtered_lines = []
222 # The IPython0 prefilters sometime produce output. We need to
216 # The IPython0 prefilters sometime produce output. We need to
223 # capture it.
217 # capture it.
224 self.capture_output()
218 self.capture_output()
225 self.last_result = dict(number=self.prompt_number)
219 self.last_result = dict(number=self.prompt_number)
226
220
227 try:
221 try:
228 try:
222 try:
229 for line in input_string.split('\n'):
223 for line in input_string.split('\n'):
230 pf = self.ipython0.prefilter_manager.prefilter_lines
224 pf = self.ipython0.prefilter_manager.prefilter_lines
231 filtered_lines.append(pf(line, False).rstrip())
225 filtered_lines.append(pf(line, False).rstrip())
232 except:
226 except:
233 # XXX: probably not the right thing to do.
227 # XXX: probably not the right thing to do.
234 self.ipython0.showsyntaxerror()
228 self.ipython0.showsyntaxerror()
235 self.after_execute()
229 self.after_execute()
236 finally:
230 finally:
237 self.release_output()
231 self.release_output()
238
232
239 # Clean up the trailing whitespace, to avoid indentation errors
233 # Clean up the trailing whitespace, to avoid indentation errors
240 filtered_string = '\n'.join(filtered_lines)
234 filtered_string = '\n'.join(filtered_lines)
241 return filtered_string
235 return filtered_string
242
236
243 #--------------------------------------------------------------------------
237 #--------------------------------------------------------------------------
244 # PrefilterFrontEnd interface
238 # PrefilterFrontEnd interface
245 #--------------------------------------------------------------------------
239 #--------------------------------------------------------------------------
246
240
247 def system_call(self, command_string):
241 def system_call(self, command_string):
248 """ Allows for frontend to define their own system call, to be
242 """ Allows for frontend to define their own system call, to be
249 able capture output and redirect input.
243 able capture output and redirect input.
250 """
244 """
251 return os.system(command_string)
245 return os.system(command_string)
252
246
253 def do_exit(self):
247 def do_exit(self):
254 """ Exit the shell, cleanup and save the history.
248 """ Exit the shell, cleanup and save the history.
255 """
249 """
256 self.ipython0.atexit_operations()
250 self.ipython0.atexit_operations()
257
251
258 def _get_completion_text(self, line):
252 def _get_completion_text(self, line):
259 """ Returns the text to be completed by breaking the line at specified
253 """ Returns the text to be completed by breaking the line at specified
260 delimiters.
254 delimiters.
261 """
255 """
262 # Break at: spaces, '=', all parentheses (except if balanced).
256 # Break at: spaces, '=', all parentheses (except if balanced).
263 # FIXME2: In the future, we need to make the implementation similar to
257 # FIXME2: In the future, we need to make the implementation similar to
264 # that in the 'pyreadline' module (modes/basemode.py) where we break at
258 # that in the 'pyreadline' module (modes/basemode.py) where we break at
265 # each delimiter and try to complete the residual line, until we get a
259 # each delimiter and try to complete the residual line, until we get a
266 # successful list of completions.
260 # successful list of completions.
267 expression = '\s|=|,|:|\((?!.*\))|\[(?!.*\])|\{(?!.*\})'
261 expression = '\s|=|,|:|\((?!.*\))|\[(?!.*\])|\{(?!.*\})'
268 complete_sep = re.compile(expression)
262 complete_sep = re.compile(expression)
269 text = complete_sep.split(line)[-1]
263 text = complete_sep.split(line)[-1]
270 return text
264 return text
@@ -1,272 +1,269 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 Test process execution and IO redirection.
3 Test process execution and IO redirection.
4 """
4 """
5
5
6 __docformat__ = "restructuredtext en"
6 __docformat__ = "restructuredtext en"
7
7
8 #-------------------------------------------------------------------------------
8 #-------------------------------------------------------------------------------
9 # Copyright (C) 2008 The IPython Development Team
9 # Copyright (C) 2008 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is
11 # Distributed under the terms of the BSD License. The full license is
12 # in the file COPYING, distributed as part of this software.
12 # in the file COPYING, distributed as part of this software.
13 #-------------------------------------------------------------------------------
13 #-------------------------------------------------------------------------------
14
14
15 from copy import copy, deepcopy
15 from copy import copy, deepcopy
16 from cStringIO import StringIO
16 from cStringIO import StringIO
17 import string
17 import string
18 import sys
18 import sys
19
19
20 from nose.tools import assert_equal
20 from nose.tools import assert_equal
21
21
22 from IPython.frontend.prefilterfrontend import PrefilterFrontEnd
22 from IPython.frontend.prefilterfrontend import PrefilterFrontEnd
23 from IPython.core.ipapi import get as get_ipython0
23 from IPython.core.ipapi import get as get_ipython0
24 from IPython.testing.plugin.ipdoctest import default_argv
24 from IPython.testing.plugin.ipdoctest import default_argv
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Support utilities
27 # Support utilities
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 class TestPrefilterFrontEnd(PrefilterFrontEnd):
30 class TestPrefilterFrontEnd(PrefilterFrontEnd):
31
31
32 input_prompt_template = string.Template('')
32 input_prompt_template = string.Template('')
33 output_prompt_template = string.Template('')
33 output_prompt_template = string.Template('')
34 banner = ''
34 banner = ''
35
35
36 def __init__(self):
36 def __init__(self):
37 self.out = StringIO()
37 self.out = StringIO()
38 PrefilterFrontEnd.__init__(self,argv=default_argv())
38 PrefilterFrontEnd.__init__(self,argv=default_argv())
39 # Some more code for isolation (yeah, crazy)
39 # Some more code for isolation (yeah, crazy)
40 self._on_enter()
40 self._on_enter()
41 self.out.flush()
41 self.out.flush()
42 self.out.reset()
42 self.out.reset()
43 self.out.truncate()
43 self.out.truncate()
44
44
45 def write(self, string, *args, **kwargs):
45 def write(self, string, *args, **kwargs):
46 self.out.write(string)
46 self.out.write(string)
47
47
48 def _on_enter(self):
48 def _on_enter(self):
49 self.input_buffer += '\n'
49 self.input_buffer += '\n'
50 PrefilterFrontEnd._on_enter(self)
50 PrefilterFrontEnd._on_enter(self)
51
51
52
52
53 def isolate_ipython0(func):
53 def isolate_ipython0(func):
54 """ Decorator to isolate execution that involves an iptyhon0.
54 """ Decorator to isolate execution that involves an iptyhon0.
55
55
56 Notes
56 Notes
57 -----
57 -----
58
58
59 Apply only to functions with no arguments. Nose skips functions
59 Apply only to functions with no arguments. Nose skips functions
60 with arguments.
60 with arguments.
61 """
61 """
62 def my_func():
62 def my_func():
63 ip0 = get_ipython0()
63 ip0 = get_ipython0()
64 if ip0 is None:
64 if ip0 is None:
65 return func()
65 return func()
66 # We have a real ipython running...
66 # We have a real ipython running...
67 user_ns = ip0.user_ns
67 user_ns = ip0.user_ns
68 user_global_ns = ip0.user_global_ns
68 user_global_ns = ip0.user_global_ns
69
69
70 # Previously the isolation was attempted with a deep copy of the user
70 # Previously the isolation was attempted with a deep copy of the user
71 # dicts, but we found cases where this didn't work correctly. I'm not
71 # dicts, but we found cases where this didn't work correctly. I'm not
72 # quite sure why, but basically it did damage the user namespace, such
72 # quite sure why, but basically it did damage the user namespace, such
73 # that later tests stopped working correctly. Instead we use a simpler
73 # that later tests stopped working correctly. Instead we use a simpler
74 # approach, just computing the list of added keys to the namespace and
74 # approach, just computing the list of added keys to the namespace and
75 # eliminating those afterwards. Existing keys that may have been
75 # eliminating those afterwards. Existing keys that may have been
76 # modified remain modified. So far this has proven to be robust.
76 # modified remain modified. So far this has proven to be robust.
77
77
78 # Compute set of old local/global keys
78 # Compute set of old local/global keys
79 old_locals = set(user_ns.keys())
79 old_locals = set(user_ns.keys())
80 old_globals = set(user_global_ns.keys())
80 old_globals = set(user_global_ns.keys())
81 try:
81 try:
82 out = func()
82 out = func()
83 finally:
83 finally:
84 # Find new keys, and if any, remove them
84 # Find new keys, and if any, remove them
85 new_locals = set(user_ns.keys()) - old_locals
85 new_locals = set(user_ns.keys()) - old_locals
86 new_globals = set(user_global_ns.keys()) - old_globals
86 new_globals = set(user_global_ns.keys()) - old_globals
87 for k in new_locals:
87 for k in new_locals:
88 del user_ns[k]
88 del user_ns[k]
89 for k in new_globals:
89 for k in new_globals:
90 del user_global_ns[k]
90 del user_global_ns[k]
91 # Undo the hack at creation of PrefilterFrontEnd
92 from IPython.core import iplib
93 iplib.InteractiveShell.isthreaded = False
94 return out
91 return out
95
92
96 my_func.__name__ = func.__name__
93 my_func.__name__ = func.__name__
97 return my_func
94 return my_func
98
95
99 #-----------------------------------------------------------------------------
96 #-----------------------------------------------------------------------------
100 # Tests
97 # Tests
101 #-----------------------------------------------------------------------------
98 #-----------------------------------------------------------------------------
102
99
103 @isolate_ipython0
100 @isolate_ipython0
104 def test_execution():
101 def test_execution():
105 """ Test execution of a command.
102 """ Test execution of a command.
106 """
103 """
107 f = TestPrefilterFrontEnd()
104 f = TestPrefilterFrontEnd()
108 f.input_buffer = 'print(1)'
105 f.input_buffer = 'print(1)'
109 f._on_enter()
106 f._on_enter()
110 out_value = f.out.getvalue()
107 out_value = f.out.getvalue()
111 assert_equal(out_value, '1\n')
108 assert_equal(out_value, '1\n')
112
109
113
110
114 @isolate_ipython0
111 @isolate_ipython0
115 def test_multiline():
112 def test_multiline():
116 """ Test execution of a multiline command.
113 """ Test execution of a multiline command.
117 """
114 """
118 f = TestPrefilterFrontEnd()
115 f = TestPrefilterFrontEnd()
119 f.input_buffer = 'if True:'
116 f.input_buffer = 'if True:'
120 f._on_enter()
117 f._on_enter()
121 f.input_buffer += 'print 1'
118 f.input_buffer += 'print 1'
122 f._on_enter()
119 f._on_enter()
123 out_value = f.out.getvalue()
120 out_value = f.out.getvalue()
124 yield assert_equal, out_value, ''
121 yield assert_equal, out_value, ''
125 f._on_enter()
122 f._on_enter()
126 out_value = f.out.getvalue()
123 out_value = f.out.getvalue()
127 yield assert_equal, out_value, '1\n'
124 yield assert_equal, out_value, '1\n'
128 f = TestPrefilterFrontEnd()
125 f = TestPrefilterFrontEnd()
129 f.input_buffer='(1 +'
126 f.input_buffer='(1 +'
130 f._on_enter()
127 f._on_enter()
131 f.input_buffer += '0)'
128 f.input_buffer += '0)'
132 f._on_enter()
129 f._on_enter()
133 out_value = f.out.getvalue()
130 out_value = f.out.getvalue()
134 yield assert_equal, out_value, ''
131 yield assert_equal, out_value, ''
135 f._on_enter()
132 f._on_enter()
136 out_value = f.out.getvalue()
133 out_value = f.out.getvalue()
137 yield assert_equal, out_value, '1\n'
134 yield assert_equal, out_value, '1\n'
138
135
139
136
140 @isolate_ipython0
137 @isolate_ipython0
141 def test_capture():
138 def test_capture():
142 """ Test the capture of output in different channels.
139 """ Test the capture of output in different channels.
143 """
140 """
144 # Test on the OS-level stdout, stderr.
141 # Test on the OS-level stdout, stderr.
145 f = TestPrefilterFrontEnd()
142 f = TestPrefilterFrontEnd()
146 f.input_buffer = \
143 f.input_buffer = \
147 'import os; out=os.fdopen(1, "w"); out.write("1") ; out.flush()'
144 'import os; out=os.fdopen(1, "w"); out.write("1") ; out.flush()'
148 f._on_enter()
145 f._on_enter()
149 out_value = f.out.getvalue()
146 out_value = f.out.getvalue()
150 yield assert_equal, out_value, '1'
147 yield assert_equal, out_value, '1'
151 f = TestPrefilterFrontEnd()
148 f = TestPrefilterFrontEnd()
152 f.input_buffer = \
149 f.input_buffer = \
153 'import os; out=os.fdopen(2, "w"); out.write("1") ; out.flush()'
150 'import os; out=os.fdopen(2, "w"); out.write("1") ; out.flush()'
154 f._on_enter()
151 f._on_enter()
155 out_value = f.out.getvalue()
152 out_value = f.out.getvalue()
156 yield assert_equal, out_value, '1'
153 yield assert_equal, out_value, '1'
157
154
158
155
159 @isolate_ipython0
156 @isolate_ipython0
160 def test_magic():
157 def test_magic():
161 """ Test the magic expansion and history.
158 """ Test the magic expansion and history.
162
159
163 This test is fairly fragile and will break when magics change.
160 This test is fairly fragile and will break when magics change.
164 """
161 """
165 f = TestPrefilterFrontEnd()
162 f = TestPrefilterFrontEnd()
166 # Before checking the interactive namespace, make sure it's clear (it can
163 # Before checking the interactive namespace, make sure it's clear (it can
167 # otherwise pick up things stored in the user's local db)
164 # otherwise pick up things stored in the user's local db)
168 f.input_buffer += '%reset -f'
165 f.input_buffer += '%reset -f'
169 f._on_enter()
166 f._on_enter()
170 f.complete_current_input()
167 f.complete_current_input()
171 # Now, run the %who magic and check output
168 # Now, run the %who magic and check output
172 f.input_buffer += '%who'
169 f.input_buffer += '%who'
173 f._on_enter()
170 f._on_enter()
174 out_value = f.out.getvalue()
171 out_value = f.out.getvalue()
175 assert_equal(out_value, 'In\tOut\tget_ipython\t\n')
172 assert_equal(out_value, 'In\tOut\tget_ipython\t\n')
176
173
177
174
178 @isolate_ipython0
175 @isolate_ipython0
179 def test_help():
176 def test_help():
180 """ Test object inspection.
177 """ Test object inspection.
181 """
178 """
182 f = TestPrefilterFrontEnd()
179 f = TestPrefilterFrontEnd()
183 f.input_buffer += "def f():"
180 f.input_buffer += "def f():"
184 f._on_enter()
181 f._on_enter()
185 f.input_buffer += "'foobar'"
182 f.input_buffer += "'foobar'"
186 f._on_enter()
183 f._on_enter()
187 f.input_buffer += "pass"
184 f.input_buffer += "pass"
188 f._on_enter()
185 f._on_enter()
189 f._on_enter()
186 f._on_enter()
190 f.input_buffer += "f?"
187 f.input_buffer += "f?"
191 f._on_enter()
188 f._on_enter()
192 assert 'traceback' not in f.last_result
189 assert 'traceback' not in f.last_result
193 ## XXX: ipython doctest magic breaks this. I have no clue why
190 ## XXX: ipython doctest magic breaks this. I have no clue why
194 #out_value = f.out.getvalue()
191 #out_value = f.out.getvalue()
195 #assert out_value.split()[-1] == 'foobar'
192 #assert out_value.split()[-1] == 'foobar'
196
193
197
194
198 @isolate_ipython0
195 @isolate_ipython0
199 def test_completion_simple():
196 def test_completion_simple():
200 """ Test command-line completion on trivial examples.
197 """ Test command-line completion on trivial examples.
201 """
198 """
202 f = TestPrefilterFrontEnd()
199 f = TestPrefilterFrontEnd()
203 f.input_buffer = 'zzza = 1'
200 f.input_buffer = 'zzza = 1'
204 f._on_enter()
201 f._on_enter()
205 f.input_buffer = 'zzzb = 2'
202 f.input_buffer = 'zzzb = 2'
206 f._on_enter()
203 f._on_enter()
207 f.input_buffer = 'zz'
204 f.input_buffer = 'zz'
208 f.complete_current_input()
205 f.complete_current_input()
209 out_value = f.out.getvalue()
206 out_value = f.out.getvalue()
210 yield assert_equal, out_value, '\nzzza zzzb '
207 yield assert_equal, out_value, '\nzzza zzzb '
211 yield assert_equal, f.input_buffer, 'zzz'
208 yield assert_equal, f.input_buffer, 'zzz'
212
209
213
210
214 @isolate_ipython0
211 @isolate_ipython0
215 def test_completion_parenthesis():
212 def test_completion_parenthesis():
216 """ Test command-line completion when a parenthesis is open.
213 """ Test command-line completion when a parenthesis is open.
217 """
214 """
218 f = TestPrefilterFrontEnd()
215 f = TestPrefilterFrontEnd()
219 f.input_buffer = 'zzza = 1'
216 f.input_buffer = 'zzza = 1'
220 f._on_enter()
217 f._on_enter()
221 f.input_buffer = 'zzzb = 2'
218 f.input_buffer = 'zzzb = 2'
222 f._on_enter()
219 f._on_enter()
223 f.input_buffer = 'map(zz'
220 f.input_buffer = 'map(zz'
224 f.complete_current_input()
221 f.complete_current_input()
225 out_value = f.out.getvalue()
222 out_value = f.out.getvalue()
226 yield assert_equal, out_value, '\nzzza zzzb '
223 yield assert_equal, out_value, '\nzzza zzzb '
227 yield assert_equal, f.input_buffer, 'map(zzz'
224 yield assert_equal, f.input_buffer, 'map(zzz'
228
225
229
226
230 @isolate_ipython0
227 @isolate_ipython0
231 def test_completion_indexing():
228 def test_completion_indexing():
232 """ Test command-line completion when indexing on objects.
229 """ Test command-line completion when indexing on objects.
233 """
230 """
234 f = TestPrefilterFrontEnd()
231 f = TestPrefilterFrontEnd()
235 f.input_buffer = 'a = [0]'
232 f.input_buffer = 'a = [0]'
236 f._on_enter()
233 f._on_enter()
237 f.input_buffer = 'a[0].'
234 f.input_buffer = 'a[0].'
238 f.complete_current_input()
235 f.complete_current_input()
239
236
240 if sys.version_info[:2] >= (2,6):
237 if sys.version_info[:2] >= (2,6):
241 # In Python 2.6, ints picked up a few non __ methods, so now there are
238 # In Python 2.6, ints picked up a few non __ methods, so now there are
242 # no completions.
239 # no completions.
243 assert_equal(f.input_buffer, 'a[0].')
240 assert_equal(f.input_buffer, 'a[0].')
244 else:
241 else:
245 # Right answer for 2.4/2.5
242 # Right answer for 2.4/2.5
246 assert_equal(f.input_buffer, 'a[0].__')
243 assert_equal(f.input_buffer, 'a[0].__')
247
244
248
245
249 @isolate_ipython0
246 @isolate_ipython0
250 def test_completion_equal():
247 def test_completion_equal():
251 """ Test command-line completion when the delimiter is "=", not " ".
248 """ Test command-line completion when the delimiter is "=", not " ".
252 """
249 """
253 f = TestPrefilterFrontEnd()
250 f = TestPrefilterFrontEnd()
254 f.input_buffer = 'a=1.'
251 f.input_buffer = 'a=1.'
255 f.complete_current_input()
252 f.complete_current_input()
256 if sys.version_info[:2] >= (2,6):
253 if sys.version_info[:2] >= (2,6):
257 # In Python 2.6, ints picked up a few non __ methods, so now there are
254 # In Python 2.6, ints picked up a few non __ methods, so now there are
258 # no completions.
255 # no completions.
259 assert_equal(f.input_buffer, 'a=1.')
256 assert_equal(f.input_buffer, 'a=1.')
260 else:
257 else:
261 # Right answer for 2.4/2.5
258 # Right answer for 2.4/2.5
262 assert_equal(f.input_buffer, 'a=1.__')
259 assert_equal(f.input_buffer, 'a=1.__')
263
260
264
261
265 if __name__ == '__main__':
262 if __name__ == '__main__':
266 test_magic()
263 test_magic()
267 test_help()
264 test_help()
268 test_execution()
265 test_execution()
269 test_multiline()
266 test_multiline()
270 test_capture()
267 test_capture()
271 test_completion_simple()
268 test_completion_simple()
272 test_completion_complex()
269 test_completion_complex()
General Comments 0
You need to be logged in to leave comments. Login now