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