##// END OF EJS Templates
Renamed @register_magics to @magics_class to avoid confusion....
Fernando Perez -
Show More
@@ -1,437 +1,437 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """Magic functions for InteractiveShell.
2 """Magic functions for InteractiveShell.
3 """
3 """
4
4
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu>
7 # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2008 The IPython Development Team
8 # Copyright (C) 2008 The IPython Development Team
9
9
10 # Distributed under the terms of the BSD License. The full license is in
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
11 # the file COPYING, distributed as part of this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 # Stdlib
17 # Stdlib
18 import os
18 import os
19 import re
19 import re
20 import sys
20 import sys
21 import types
21 import types
22 from getopt import getopt, GetoptError
22 from getopt import getopt, GetoptError
23
23
24 # Our own
24 # Our own
25 from IPython.config.configurable import Configurable
25 from IPython.config.configurable import Configurable
26 from IPython.core import oinspect
26 from IPython.core import oinspect
27 from IPython.core.error import UsageError
27 from IPython.core.error import UsageError
28 from IPython.core.prefilter import ESC_MAGIC
28 from IPython.core.prefilter import ESC_MAGIC
29 from IPython.external.decorator import decorator
29 from IPython.external.decorator import decorator
30 from IPython.utils.ipstruct import Struct
30 from IPython.utils.ipstruct import Struct
31 from IPython.utils.process import arg_split
31 from IPython.utils.process import arg_split
32 from IPython.utils.traitlets import Bool, Dict, Instance
32 from IPython.utils.traitlets import Bool, Dict, Instance
33 from IPython.utils.warn import error, warn
33 from IPython.utils.warn import error, warn
34
34
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36 # Globals
36 # Globals
37 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
38
38
39 # A dict we'll use for each class that has magics, used as temporary storage to
39 # A dict we'll use for each class that has magics, used as temporary storage to
40 # pass information between the @line/cell_magic method decorators and the
40 # pass information between the @line/cell_magic method decorators and the
41 # @register_magics class decorator, because the method decorators have no
41 # @magics_class class decorator, because the method decorators have no
42 # access to the class when they run. See for more details:
42 # access to the class when they run. See for more details:
43 # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class
43 # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class
44
44
45 magics = dict(line={}, cell={})
45 magics = dict(line={}, cell={})
46
46
47 magic_types = ('line', 'cell')
47 magic_types = ('line', 'cell')
48 magic_spec = ('line', 'cell', 'line_cell')
48 magic_spec = ('line', 'cell', 'line_cell')
49
49
50 #-----------------------------------------------------------------------------
50 #-----------------------------------------------------------------------------
51 # Utility classes and functions
51 # Utility classes and functions
52 #-----------------------------------------------------------------------------
52 #-----------------------------------------------------------------------------
53
53
54 class Bunch: pass
54 class Bunch: pass
55
55
56
56
57 def on_off(tag):
57 def on_off(tag):
58 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
58 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
59 return ['OFF','ON'][tag]
59 return ['OFF','ON'][tag]
60
60
61
61
62 def compress_dhist(dh):
62 def compress_dhist(dh):
63 head, tail = dh[:-10], dh[-10:]
63 head, tail = dh[:-10], dh[-10:]
64
64
65 newhead = []
65 newhead = []
66 done = set()
66 done = set()
67 for h in head:
67 for h in head:
68 if h in done:
68 if h in done:
69 continue
69 continue
70 newhead.append(h)
70 newhead.append(h)
71 done.add(h)
71 done.add(h)
72
72
73 return newhead + tail
73 return newhead + tail
74
74
75
75
76 def needs_local_scope(func):
76 def needs_local_scope(func):
77 """Decorator to mark magic functions which need to local scope to run."""
77 """Decorator to mark magic functions which need to local scope to run."""
78 func.needs_local_scope = True
78 func.needs_local_scope = True
79 return func
79 return func
80
80
81 #-----------------------------------------------------------------------------
81 #-----------------------------------------------------------------------------
82 # Class and method decorators for registering magics
82 # Class and method decorators for registering magics
83 #-----------------------------------------------------------------------------
83 #-----------------------------------------------------------------------------
84
84
85 def register_magics(cls):
85 def magics_class(cls):
86 cls.registered = True
86 cls.registered = True
87 cls.magics = dict(line = magics['line'],
87 cls.magics = dict(line = magics['line'],
88 cell = magics['cell'])
88 cell = magics['cell'])
89 magics['line'] = {}
89 magics['line'] = {}
90 magics['cell'] = {}
90 magics['cell'] = {}
91 return cls
91 return cls
92
92
93
93
94 def record_magic(dct, mtype, mname, func):
94 def record_magic(dct, mtype, mname, func):
95 if mtype == 'line_cell':
95 if mtype == 'line_cell':
96 dct['line'][mname] = dct['cell'][mname] = func
96 dct['line'][mname] = dct['cell'][mname] = func
97 else:
97 else:
98 dct[mtype][mname] = func
98 dct[mtype][mname] = func
99
99
100
100
101 def validate_type(magic_type):
101 def validate_type(magic_type):
102 if magic_type not in magic_spec:
102 if magic_type not in magic_spec:
103 raise ValueError('magic_type must be one of %s, %s given' %
103 raise ValueError('magic_type must be one of %s, %s given' %
104 magic_types, magic_type)
104 magic_types, magic_type)
105
105
106
106
107 def _magic_marker(magic_type):
107 def _magic_marker(magic_type):
108 validate_type(magic_type)
108 validate_type(magic_type)
109
109
110 # This is a closure to capture the magic_type. We could also use a class,
110 # This is a closure to capture the magic_type. We could also use a class,
111 # but it's overkill for just that one bit of state.
111 # but it's overkill for just that one bit of state.
112 def magic_deco(arg):
112 def magic_deco(arg):
113 call = lambda f, *a, **k: f(*a, **k)
113 call = lambda f, *a, **k: f(*a, **k)
114
114
115 if callable(arg):
115 if callable(arg):
116 # "Naked" decorator call (just @foo, no args)
116 # "Naked" decorator call (just @foo, no args)
117 func = arg
117 func = arg
118 name = func.func_name
118 name = func.func_name
119 func.magic_name = name
119 func.magic_name = name
120 retval = decorator(call, func)
120 retval = decorator(call, func)
121 record_magic(magics, magic_type, name, name)
121 record_magic(magics, magic_type, name, name)
122 elif isinstance(arg, basestring):
122 elif isinstance(arg, basestring):
123 # Decorator called with arguments (@foo('bar'))
123 # Decorator called with arguments (@foo('bar'))
124 name = arg
124 name = arg
125 def mark(func, *a, **kw):
125 def mark(func, *a, **kw):
126 func.magic_name = name
126 func.magic_name = name
127 record_magic(magics, magic_type, name, func.func_name)
127 record_magic(magics, magic_type, name, func.func_name)
128 return decorator(call, func)
128 return decorator(call, func)
129 retval = mark
129 retval = mark
130 else:
130 else:
131 raise ValueError("Decorator can only be called with "
131 raise ValueError("Decorator can only be called with "
132 "string or function")
132 "string or function")
133 return retval
133 return retval
134
134
135 return magic_deco
135 return magic_deco
136
136
137
137
138 def _function_magic_marker(magic_type):
138 def _function_magic_marker(magic_type):
139 validate_type(magic_type)
139 validate_type(magic_type)
140
140
141 # This is a closure to capture the magic_type. We could also use a class,
141 # This is a closure to capture the magic_type. We could also use a class,
142 # but it's overkill for just that one bit of state.
142 # but it's overkill for just that one bit of state.
143 def magic_deco(arg):
143 def magic_deco(arg):
144 call = lambda f, *a, **k: f(*a, **k)
144 call = lambda f, *a, **k: f(*a, **k)
145
145
146 # Find get_ipython() in the caller's namespace
146 # Find get_ipython() in the caller's namespace
147 caller = sys._getframe(1)
147 caller = sys._getframe(1)
148 for ns in ['f_locals', 'f_globals', 'f_builtins']:
148 for ns in ['f_locals', 'f_globals', 'f_builtins']:
149 get_ipython = getattr(caller, ns).get('get_ipython')
149 get_ipython = getattr(caller, ns).get('get_ipython')
150 if get_ipython is not None:
150 if get_ipython is not None:
151 break
151 break
152 else:
152 else:
153 raise('Decorator can only run in context where `get_ipython` exists')
153 raise('Decorator can only run in context where `get_ipython` exists')
154
154
155 ip = get_ipython()
155 ip = get_ipython()
156
156
157 if callable(arg):
157 if callable(arg):
158 # "Naked" decorator call (just @foo, no args)
158 # "Naked" decorator call (just @foo, no args)
159 func = arg
159 func = arg
160 #name = func.func_name
160 #name = func.func_name
161 #func.magic_name = name
161 #func.magic_name = name
162 ip.register_magic_function(func)
162 ip.register_magic_function(func)
163 retval = decorator(call, func)
163 retval = decorator(call, func)
164 elif isinstance(arg, basestring):
164 elif isinstance(arg, basestring):
165 # Decorator called with arguments (@foo('bar'))
165 # Decorator called with arguments (@foo('bar'))
166 name = arg
166 name = arg
167 def mark(func, *a, **kw):
167 def mark(func, *a, **kw):
168 #func.magic_name = name
168 #func.magic_name = name
169 ip.register_magic_function(func)
169 ip.register_magic_function(func)
170 return decorator(call, func)
170 return decorator(call, func)
171 retval = mark
171 retval = mark
172 else:
172 else:
173 raise ValueError("Decorator can only be called with "
173 raise ValueError("Decorator can only be called with "
174 "string or function")
174 "string or function")
175 return retval
175 return retval
176
176
177 return magic_deco
177 return magic_deco
178
178
179
179
180 # Create the actual decorators for public use
180 # Create the actual decorators for public use
181
181
182 # These three are used to decorate methods in class definitions
182 # These three are used to decorate methods in class definitions
183 line_magic = _magic_marker('line')
183 line_magic = _magic_marker('line')
184 cell_magic = _magic_marker('cell')
184 cell_magic = _magic_marker('cell')
185 line_cell_magic = _magic_marker('line_cell')
185 line_cell_magic = _magic_marker('line_cell')
186
186
187 # These three decorate standalone functions and perform the decoration
187 # These three decorate standalone functions and perform the decoration
188 # immediately. They can only run where get_ipython() works
188 # immediately. They can only run where get_ipython() works
189 register_line_magic = _function_magic_marker('line')
189 register_line_magic = _function_magic_marker('line')
190 register_cell_magic = _function_magic_marker('cell')
190 register_cell_magic = _function_magic_marker('cell')
191 register_line_cell_magic = _function_magic_marker('line_cell')
191 register_line_cell_magic = _function_magic_marker('line_cell')
192
192
193 #-----------------------------------------------------------------------------
193 #-----------------------------------------------------------------------------
194 # Core Magic classes
194 # Core Magic classes
195 #-----------------------------------------------------------------------------
195 #-----------------------------------------------------------------------------
196
196
197 class MagicsManager(Configurable):
197 class MagicsManager(Configurable):
198 """Object that handles all magic-related functionality for IPython.
198 """Object that handles all magic-related functionality for IPython.
199 """
199 """
200 # Non-configurable class attributes
200 # Non-configurable class attributes
201
201
202 # A two-level dict, first keyed by magic type, then by magic function, and
202 # A two-level dict, first keyed by magic type, then by magic function, and
203 # holding the actual callable object as value. This is the dict used for
203 # holding the actual callable object as value. This is the dict used for
204 # magic function dispatch
204 # magic function dispatch
205 magics = Dict
205 magics = Dict
206
206
207 # A registry of the original objects that we've been given holding magics.
207 # A registry of the original objects that we've been given holding magics.
208 registry = Dict
208 registry = Dict
209
209
210 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
210 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
211
211
212 auto_magic = Bool
212 auto_magic = Bool
213
213
214 _auto_status = [
214 _auto_status = [
215 'Automagic is OFF, % prefix IS needed for magic functions.',
215 'Automagic is OFF, % prefix IS needed for magic functions.',
216 'Automagic is ON, % prefix IS NOT needed for magic functions.']
216 'Automagic is ON, % prefix IS NOT needed for magic functions.']
217
217
218 user_magics = Instance('IPython.core.magics.UserMagics')
218 user_magics = Instance('IPython.core.magics.UserMagics')
219
219
220 def __init__(self, shell=None, config=None, user_magics=None, **traits):
220 def __init__(self, shell=None, config=None, user_magics=None, **traits):
221
221
222 super(MagicsManager, self).__init__(shell=shell, config=config,
222 super(MagicsManager, self).__init__(shell=shell, config=config,
223 user_magics=user_magics, **traits)
223 user_magics=user_magics, **traits)
224 self.magics = dict(line={}, cell={})
224 self.magics = dict(line={}, cell={})
225 # Let's add the user_magics to the registry for uniformity, so *all*
225 # Let's add the user_magics to the registry for uniformity, so *all*
226 # registered magic containers can be found there.
226 # registered magic containers can be found there.
227 self.registry[user_magics.__class__.__name__] = user_magics
227 self.registry[user_magics.__class__.__name__] = user_magics
228
228
229 def auto_status(self):
229 def auto_status(self):
230 """Return descriptive string with automagic status."""
230 """Return descriptive string with automagic status."""
231 return self._auto_status[self.auto_magic]
231 return self._auto_status[self.auto_magic]
232
232
233 def lsmagic(self):
233 def lsmagic(self):
234 """Return a dict of currently available magic functions.
234 """Return a dict of currently available magic functions.
235
235
236 The return dict has the keys 'line' and 'cell', corresponding to the
236 The return dict has the keys 'line' and 'cell', corresponding to the
237 two types of magics we support. Each value is a list of names.
237 two types of magics we support. Each value is a list of names.
238 """
238 """
239 return self.magics
239 return self.magics
240
240
241 def register(self, *magic_objects):
241 def register(self, *magic_objects):
242 """Register one or more instances of Magics.
242 """Register one or more instances of Magics.
243 """
243 """
244 # Start by validating them to ensure they have all had their magic
244 # Start by validating them to ensure they have all had their magic
245 # methods registered at the instance level
245 # methods registered at the instance level
246 for m in magic_objects:
246 for m in magic_objects:
247 if not m.registered:
247 if not m.registered:
248 raise ValueError("Class of magics %r was constructed without "
248 raise ValueError("Class of magics %r was constructed without "
249 "the @register_macics class decorator")
249 "the @register_macics class decorator")
250 if type(m) is type:
250 if type(m) is type:
251 # If we're given an uninstantiated class
251 # If we're given an uninstantiated class
252 m = m(self.shell)
252 m = m(self.shell)
253
253
254 # Now that we have an instance, we can register it and update the
254 # Now that we have an instance, we can register it and update the
255 # table of callables
255 # table of callables
256 self.registry[m.__class__.__name__] = m
256 self.registry[m.__class__.__name__] = m
257 for mtype in magic_types:
257 for mtype in magic_types:
258 self.magics[mtype].update(m.magics[mtype])
258 self.magics[mtype].update(m.magics[mtype])
259
259
260 def register_function(self, func, magic_type='line', magic_name=None):
260 def register_function(self, func, magic_type='line', magic_name=None):
261 """Expose a standalone function as magic function for ipython.
261 """Expose a standalone function as magic function for ipython.
262 """
262 """
263
263
264 # Create the new method in the user_magics and register it in the
264 # Create the new method in the user_magics and register it in the
265 # global table
265 # global table
266 validate_type(magic_type)
266 validate_type(magic_type)
267 magic_name = func.func_name if magic_name is None else magic_name
267 magic_name = func.func_name if magic_name is None else magic_name
268 setattr(self.user_magics, magic_name, func)
268 setattr(self.user_magics, magic_name, func)
269 record_magic(self.magics, magic_type, magic_name, func)
269 record_magic(self.magics, magic_type, magic_name, func)
270
270
271 def define_magic(self, name, func):
271 def define_magic(self, name, func):
272 """Support for deprecated API.
272 """Support for deprecated API.
273
273
274 This method exists only to support the old-style definition of magics.
274 This method exists only to support the old-style definition of magics.
275 It will eventually be removed. Deliberately not documented further.
275 It will eventually be removed. Deliberately not documented further.
276 """
276 """
277 meth = types.MethodType(func, self.user_magics)
277 meth = types.MethodType(func, self.user_magics)
278 setattr(self.user_magics, name, meth)
278 setattr(self.user_magics, name, meth)
279 record_magic(self.magics, 'line', name, meth)
279 record_magic(self.magics, 'line', name, meth)
280
280
281 # Key base class that provides the central functionality for magics.
281 # Key base class that provides the central functionality for magics.
282
282
283 class Magics(object):
283 class Magics(object):
284 """Base class for implementing magic functions.
284 """Base class for implementing magic functions.
285
285
286 Shell functions which can be reached as %function_name. All magic
286 Shell functions which can be reached as %function_name. All magic
287 functions should accept a string, which they can parse for their own
287 functions should accept a string, which they can parse for their own
288 needs. This can make some functions easier to type, eg `%cd ../`
288 needs. This can make some functions easier to type, eg `%cd ../`
289 vs. `%cd("../")`
289 vs. `%cd("../")`
290
290
291 Classes providing magic functions need to subclass this class, and they
291 Classes providing magic functions need to subclass this class, and they
292 MUST:
292 MUST:
293
293
294 - Use the method decorators `@line_magic` and `@cell_magic` to decorate
294 - Use the method decorators `@line_magic` and `@cell_magic` to decorate
295 individual methods as magic functions, AND
295 individual methods as magic functions, AND
296
296
297 - Use the class decorator `@register_magics` to ensure that the magic
297 - Use the class decorator `@magics_class` to ensure that the magic
298 methods are properly registered at the instance level upon instance
298 methods are properly registered at the instance level upon instance
299 initialization.
299 initialization.
300
300
301 See :mod:`magic_functions` for examples of actual implementation classes.
301 See :mod:`magic_functions` for examples of actual implementation classes.
302 """
302 """
303 # Dict holding all command-line options for each magic.
303 # Dict holding all command-line options for each magic.
304 options_table = None
304 options_table = None
305 # Dict for the mapping of magic names to methods, set by class decorator
305 # Dict for the mapping of magic names to methods, set by class decorator
306 magics = None
306 magics = None
307 # Flag to check that the class decorator was properly applied
307 # Flag to check that the class decorator was properly applied
308 registered = False
308 registered = False
309 # Instance of IPython shell
309 # Instance of IPython shell
310 shell = None
310 shell = None
311
311
312 def __init__(self, shell):
312 def __init__(self, shell):
313 if not(self.__class__.registered):
313 if not(self.__class__.registered):
314 raise ValueError('Magics subclass without registration - '
314 raise ValueError('Magics subclass without registration - '
315 'did you forget to apply @register_magics?')
315 'did you forget to apply @magics_class?')
316 self.shell = shell
316 self.shell = shell
317 self.options_table = {}
317 self.options_table = {}
318 # The method decorators are run when the instance doesn't exist yet, so
318 # The method decorators are run when the instance doesn't exist yet, so
319 # they can only record the names of the methods they are supposed to
319 # they can only record the names of the methods they are supposed to
320 # grab. Only now, that the instance exists, can we create the proper
320 # grab. Only now, that the instance exists, can we create the proper
321 # mapping to bound methods. So we read the info off the original names
321 # mapping to bound methods. So we read the info off the original names
322 # table and replace each method name by the actual bound method.
322 # table and replace each method name by the actual bound method.
323 for mtype in magic_types:
323 for mtype in magic_types:
324 tab = self.magics[mtype]
324 tab = self.magics[mtype]
325 # must explicitly use keys, as we're mutating this puppy
325 # must explicitly use keys, as we're mutating this puppy
326 for magic_name in tab.keys():
326 for magic_name in tab.keys():
327 meth_name = tab[magic_name]
327 meth_name = tab[magic_name]
328 if isinstance(meth_name, basestring):
328 if isinstance(meth_name, basestring):
329 tab[magic_name] = getattr(self, meth_name)
329 tab[magic_name] = getattr(self, meth_name)
330
330
331 def arg_err(self,func):
331 def arg_err(self,func):
332 """Print docstring if incorrect arguments were passed"""
332 """Print docstring if incorrect arguments were passed"""
333 print 'Error in arguments:'
333 print 'Error in arguments:'
334 print oinspect.getdoc(func)
334 print oinspect.getdoc(func)
335
335
336 def format_latex(self, strng):
336 def format_latex(self, strng):
337 """Format a string for latex inclusion."""
337 """Format a string for latex inclusion."""
338
338
339 # Characters that need to be escaped for latex:
339 # Characters that need to be escaped for latex:
340 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
340 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
341 # Magic command names as headers:
341 # Magic command names as headers:
342 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
342 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
343 re.MULTILINE)
343 re.MULTILINE)
344 # Magic commands
344 # Magic commands
345 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
345 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
346 re.MULTILINE)
346 re.MULTILINE)
347 # Paragraph continue
347 # Paragraph continue
348 par_re = re.compile(r'\\$',re.MULTILINE)
348 par_re = re.compile(r'\\$',re.MULTILINE)
349
349
350 # The "\n" symbol
350 # The "\n" symbol
351 newline_re = re.compile(r'\\n')
351 newline_re = re.compile(r'\\n')
352
352
353 # Now build the string for output:
353 # Now build the string for output:
354 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
354 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
355 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
355 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
356 strng)
356 strng)
357 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
357 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
358 strng = par_re.sub(r'\\\\',strng)
358 strng = par_re.sub(r'\\\\',strng)
359 strng = escape_re.sub(r'\\\1',strng)
359 strng = escape_re.sub(r'\\\1',strng)
360 strng = newline_re.sub(r'\\textbackslash{}n',strng)
360 strng = newline_re.sub(r'\\textbackslash{}n',strng)
361 return strng
361 return strng
362
362
363 def parse_options(self, arg_str, opt_str, *long_opts, **kw):
363 def parse_options(self, arg_str, opt_str, *long_opts, **kw):
364 """Parse options passed to an argument string.
364 """Parse options passed to an argument string.
365
365
366 The interface is similar to that of getopt(), but it returns back a
366 The interface is similar to that of getopt(), but it returns back a
367 Struct with the options as keys and the stripped argument string still
367 Struct with the options as keys and the stripped argument string still
368 as a string.
368 as a string.
369
369
370 arg_str is quoted as a true sys.argv vector by using shlex.split.
370 arg_str is quoted as a true sys.argv vector by using shlex.split.
371 This allows us to easily expand variables, glob files, quote
371 This allows us to easily expand variables, glob files, quote
372 arguments, etc.
372 arguments, etc.
373
373
374 Options:
374 Options:
375 -mode: default 'string'. If given as 'list', the argument string is
375 -mode: default 'string'. If given as 'list', the argument string is
376 returned as a list (split on whitespace) instead of a string.
376 returned as a list (split on whitespace) instead of a string.
377
377
378 -list_all: put all option values in lists. Normally only options
378 -list_all: put all option values in lists. Normally only options
379 appearing more than once are put in a list.
379 appearing more than once are put in a list.
380
380
381 -posix (True): whether to split the input line in POSIX mode or not,
381 -posix (True): whether to split the input line in POSIX mode or not,
382 as per the conventions outlined in the shlex module from the
382 as per the conventions outlined in the shlex module from the
383 standard library."""
383 standard library."""
384
384
385 # inject default options at the beginning of the input line
385 # inject default options at the beginning of the input line
386 caller = sys._getframe(1).f_code.co_name
386 caller = sys._getframe(1).f_code.co_name
387 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
387 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
388
388
389 mode = kw.get('mode','string')
389 mode = kw.get('mode','string')
390 if mode not in ['string','list']:
390 if mode not in ['string','list']:
391 raise ValueError,'incorrect mode given: %s' % mode
391 raise ValueError,'incorrect mode given: %s' % mode
392 # Get options
392 # Get options
393 list_all = kw.get('list_all',0)
393 list_all = kw.get('list_all',0)
394 posix = kw.get('posix', os.name == 'posix')
394 posix = kw.get('posix', os.name == 'posix')
395 strict = kw.get('strict', True)
395 strict = kw.get('strict', True)
396
396
397 # Check if we have more than one argument to warrant extra processing:
397 # Check if we have more than one argument to warrant extra processing:
398 odict = {} # Dictionary with options
398 odict = {} # Dictionary with options
399 args = arg_str.split()
399 args = arg_str.split()
400 if len(args) >= 1:
400 if len(args) >= 1:
401 # If the list of inputs only has 0 or 1 thing in it, there's no
401 # If the list of inputs only has 0 or 1 thing in it, there's no
402 # need to look for options
402 # need to look for options
403 argv = arg_split(arg_str, posix, strict)
403 argv = arg_split(arg_str, posix, strict)
404 # Do regular option processing
404 # Do regular option processing
405 try:
405 try:
406 opts,args = getopt(argv,opt_str,*long_opts)
406 opts,args = getopt(argv,opt_str,*long_opts)
407 except GetoptError,e:
407 except GetoptError,e:
408 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
408 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
409 " ".join(long_opts)))
409 " ".join(long_opts)))
410 for o,a in opts:
410 for o,a in opts:
411 if o.startswith('--'):
411 if o.startswith('--'):
412 o = o[2:]
412 o = o[2:]
413 else:
413 else:
414 o = o[1:]
414 o = o[1:]
415 try:
415 try:
416 odict[o].append(a)
416 odict[o].append(a)
417 except AttributeError:
417 except AttributeError:
418 odict[o] = [odict[o],a]
418 odict[o] = [odict[o],a]
419 except KeyError:
419 except KeyError:
420 if list_all:
420 if list_all:
421 odict[o] = [a]
421 odict[o] = [a]
422 else:
422 else:
423 odict[o] = a
423 odict[o] = a
424
424
425 # Prepare opts,args for return
425 # Prepare opts,args for return
426 opts = Struct(odict)
426 opts = Struct(odict)
427 if mode == 'string':
427 if mode == 'string':
428 args = ' '.join(args)
428 args = ' '.join(args)
429
429
430 return opts,args
430 return opts,args
431
431
432 def default_option(self, fn, optstr):
432 def default_option(self, fn, optstr):
433 """Make an entry in the options_table for fn, with value optstr"""
433 """Make an entry in the options_table for fn, with value optstr"""
434
434
435 if fn not in self.lsmagic():
435 if fn not in self.lsmagic():
436 error("%s is not a magic function" % fn)
436 error("%s is not a magic function" % fn)
437 self.options_table[fn] = optstr
437 self.options_table[fn] = optstr
@@ -1,40 +1,40 b''
1 """Implementation of all the magic functions built into IPython.
1 """Implementation of all the magic functions built into IPython.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 from ..magic import Magics, register_magics
15 from ..magic import Magics, magics_class
16 from .auto import AutoMagics
16 from .auto import AutoMagics
17 from .basic import BasicMagics
17 from .basic import BasicMagics
18 from .code import CodeMagics, MacroToEdit
18 from .code import CodeMagics, MacroToEdit
19 from .config import ConfigMagics
19 from .config import ConfigMagics
20 from .deprecated import DeprecatedMagics
20 from .deprecated import DeprecatedMagics
21 from .execution import ExecutionMagics
21 from .execution import ExecutionMagics
22 from .extension import ExtensionMagics
22 from .extension import ExtensionMagics
23 from .history import HistoryMagics
23 from .history import HistoryMagics
24 from .logging import LoggingMagics
24 from .logging import LoggingMagics
25 from .namespace import NamespaceMagics
25 from .namespace import NamespaceMagics
26 from .osm import OSMagics
26 from .osm import OSMagics
27 from .pylab import PylabMagics
27 from .pylab import PylabMagics
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Magic implementation classes
30 # Magic implementation classes
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 @register_magics
33 @magics_class
34 class UserMagics(Magics):
34 class UserMagics(Magics):
35 """Placeholder for user-defined magics to be added at runtime.
35 """Placeholder for user-defined magics to be added at runtime.
36
36
37 All magics are eventually merged into a single namespace at runtime, but we
37 All magics are eventually merged into a single namespace at runtime, but we
38 use this class to isolate the magics defined dynamically by the user into
38 use this class to isolate the magics defined dynamically by the user into
39 their own class.
39 their own class.
40 """
40 """
@@ -1,128 +1,128 b''
1 """Implementation of magic functions that control various automatic behaviors.
1 """Implementation of magic functions that control various automatic behaviors.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Our own packages
15 # Our own packages
16 from IPython.core.magic import Bunch, Magics, register_magics, line_magic
16 from IPython.core.magic import Bunch, Magics, magics_class, line_magic
17 from IPython.testing.skipdoctest import skip_doctest
17 from IPython.testing.skipdoctest import skip_doctest
18 from IPython.utils.warn import error
18 from IPython.utils.warn import error
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Magic implementation classes
21 # Magic implementation classes
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23
23
24 @register_magics
24 @magics_class
25 class AutoMagics(Magics):
25 class AutoMagics(Magics):
26 """Magics that control various autoX behaviors."""
26 """Magics that control various autoX behaviors."""
27
27
28 def __init__(self, shell):
28 def __init__(self, shell):
29 super(AutoMagics, self).__init__(shell)
29 super(AutoMagics, self).__init__(shell)
30 # namespace for holding state we may need
30 # namespace for holding state we may need
31 self._magic_state = Bunch()
31 self._magic_state = Bunch()
32
32
33 @line_magic
33 @line_magic
34 def automagic(self, parameter_s=''):
34 def automagic(self, parameter_s=''):
35 """Make magic functions callable without having to type the initial %.
35 """Make magic functions callable without having to type the initial %.
36
36
37 Without argumentsl toggles on/off (when off, you must call it as
37 Without argumentsl toggles on/off (when off, you must call it as
38 %automagic, of course). With arguments it sets the value, and you can
38 %automagic, of course). With arguments it sets the value, and you can
39 use any of (case insensitive):
39 use any of (case insensitive):
40
40
41 - on, 1, True: to activate
41 - on, 1, True: to activate
42
42
43 - off, 0, False: to deactivate.
43 - off, 0, False: to deactivate.
44
44
45 Note that magic functions have lowest priority, so if there's a
45 Note that magic functions have lowest priority, so if there's a
46 variable whose name collides with that of a magic fn, automagic won't
46 variable whose name collides with that of a magic fn, automagic won't
47 work for that function (you get the variable instead). However, if you
47 work for that function (you get the variable instead). However, if you
48 delete the variable (del var), the previously shadowed magic function
48 delete the variable (del var), the previously shadowed magic function
49 becomes visible to automagic again."""
49 becomes visible to automagic again."""
50
50
51 arg = parameter_s.lower()
51 arg = parameter_s.lower()
52 mman = self.shell.magics_manager
52 mman = self.shell.magics_manager
53 if arg in ('on', '1', 'true'):
53 if arg in ('on', '1', 'true'):
54 val = True
54 val = True
55 elif arg in ('off', '0', 'false'):
55 elif arg in ('off', '0', 'false'):
56 val = False
56 val = False
57 else:
57 else:
58 val = not mman.auto_magic
58 val = not mman.auto_magic
59 mman.auto_magic = val
59 mman.auto_magic = val
60 print '\n' + self.shell.magics_manager.auto_status()
60 print '\n' + self.shell.magics_manager.auto_status()
61
61
62 @skip_doctest
62 @skip_doctest
63 @line_magic
63 @line_magic
64 def autocall(self, parameter_s=''):
64 def autocall(self, parameter_s=''):
65 """Make functions callable without having to type parentheses.
65 """Make functions callable without having to type parentheses.
66
66
67 Usage:
67 Usage:
68
68
69 %autocall [mode]
69 %autocall [mode]
70
70
71 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
71 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
72 value is toggled on and off (remembering the previous state).
72 value is toggled on and off (remembering the previous state).
73
73
74 In more detail, these values mean:
74 In more detail, these values mean:
75
75
76 0 -> fully disabled
76 0 -> fully disabled
77
77
78 1 -> active, but do not apply if there are no arguments on the line.
78 1 -> active, but do not apply if there are no arguments on the line.
79
79
80 In this mode, you get::
80 In this mode, you get::
81
81
82 In [1]: callable
82 In [1]: callable
83 Out[1]: <built-in function callable>
83 Out[1]: <built-in function callable>
84
84
85 In [2]: callable 'hello'
85 In [2]: callable 'hello'
86 ------> callable('hello')
86 ------> callable('hello')
87 Out[2]: False
87 Out[2]: False
88
88
89 2 -> Active always. Even if no arguments are present, the callable
89 2 -> Active always. Even if no arguments are present, the callable
90 object is called::
90 object is called::
91
91
92 In [2]: float
92 In [2]: float
93 ------> float()
93 ------> float()
94 Out[2]: 0.0
94 Out[2]: 0.0
95
95
96 Note that even with autocall off, you can still use '/' at the start of
96 Note that even with autocall off, you can still use '/' at the start of
97 a line to treat the first argument on the command line as a function
97 a line to treat the first argument on the command line as a function
98 and add parentheses to it::
98 and add parentheses to it::
99
99
100 In [8]: /str 43
100 In [8]: /str 43
101 ------> str(43)
101 ------> str(43)
102 Out[8]: '43'
102 Out[8]: '43'
103
103
104 # all-random (note for auto-testing)
104 # all-random (note for auto-testing)
105 """
105 """
106
106
107 if parameter_s:
107 if parameter_s:
108 arg = int(parameter_s)
108 arg = int(parameter_s)
109 else:
109 else:
110 arg = 'toggle'
110 arg = 'toggle'
111
111
112 if not arg in (0, 1, 2,'toggle'):
112 if not arg in (0, 1, 2,'toggle'):
113 error('Valid modes: (0->Off, 1->Smart, 2->Full')
113 error('Valid modes: (0->Off, 1->Smart, 2->Full')
114 return
114 return
115
115
116 if arg in (0, 1, 2):
116 if arg in (0, 1, 2):
117 self.shell.autocall = arg
117 self.shell.autocall = arg
118 else: # toggle
118 else: # toggle
119 if self.shell.autocall:
119 if self.shell.autocall:
120 self._magic_state.autocall_save = self.shell.autocall
120 self._magic_state.autocall_save = self.shell.autocall
121 self.shell.autocall = 0
121 self.shell.autocall = 0
122 else:
122 else:
123 try:
123 try:
124 self.shell.autocall = self._magic_state.autocall_save
124 self.shell.autocall = self._magic_state.autocall_save
125 except AttributeError:
125 except AttributeError:
126 self.shell.autocall = self._magic_state.autocall_save = 1
126 self.shell.autocall = self._magic_state.autocall_save = 1
127
127
128 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
128 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
@@ -1,513 +1,513 b''
1 """Implementation of basic magic functions.
1 """Implementation of basic magic functions.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 from __future__ import print_function
14 from __future__ import print_function
15
15
16 # Stdlib
16 # Stdlib
17 import io
17 import io
18 import sys
18 import sys
19 from pprint import pformat
19 from pprint import pformat
20
20
21 # Our own packages
21 # Our own packages
22 from IPython.core.error import UsageError
22 from IPython.core.error import UsageError
23 from IPython.core.magic import Magics, register_magics, line_magic
23 from IPython.core.magic import Magics, magics_class, line_magic
24 from IPython.core.prefilter import ESC_MAGIC
24 from IPython.core.prefilter import ESC_MAGIC
25 from IPython.utils.text import format_screen
25 from IPython.utils.text import format_screen
26 from IPython.core import magic_arguments, page
26 from IPython.core import magic_arguments, page
27 from IPython.testing.skipdoctest import skip_doctest
27 from IPython.testing.skipdoctest import skip_doctest
28 from IPython.utils.ipstruct import Struct
28 from IPython.utils.ipstruct import Struct
29 from IPython.utils.path import unquote_filename
29 from IPython.utils.path import unquote_filename
30 from IPython.utils.warn import warn, error
30 from IPython.utils.warn import warn, error
31
31
32 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
33 # Magics class implementation
33 # Magics class implementation
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35
35
36 @register_magics
36 @magics_class
37 class BasicMagics(Magics):
37 class BasicMagics(Magics):
38 """Magics that provide central IPython functionality.
38 """Magics that provide central IPython functionality.
39
39
40 These are various magics that don't fit into specific categories but that
40 These are various magics that don't fit into specific categories but that
41 are all part of the base 'IPython experience'."""
41 are all part of the base 'IPython experience'."""
42
42
43 def _lsmagic(self):
43 def _lsmagic(self):
44 mesc = ESC_MAGIC
44 mesc = ESC_MAGIC
45 cesc = mesc*2
45 cesc = mesc*2
46 mman = self.shell.magics_manager
46 mman = self.shell.magics_manager
47 magics = mman.lsmagic()
47 magics = mman.lsmagic()
48 out = ['Available line magics:',
48 out = ['Available line magics:',
49 mesc + (' '+mesc).join(magics['line']),
49 mesc + (' '+mesc).join(magics['line']),
50 '',
50 '',
51 'Available cell magics:',
51 'Available cell magics:',
52 cesc + (' '+cesc).join(magics['cell']),
52 cesc + (' '+cesc).join(magics['cell']),
53 '',
53 '',
54 mman.auto_status()]
54 mman.auto_status()]
55 return '\n'.join(out)
55 return '\n'.join(out)
56
56
57 @line_magic
57 @line_magic
58 def lsmagic(self, parameter_s=''):
58 def lsmagic(self, parameter_s=''):
59 """List currently available magic functions."""
59 """List currently available magic functions."""
60 print(self._lsmagic())
60 print(self._lsmagic())
61
61
62 @line_magic
62 @line_magic
63 def magic(self, parameter_s=''):
63 def magic(self, parameter_s=''):
64 """Print information about the magic function system.
64 """Print information about the magic function system.
65
65
66 Supported formats: -latex, -brief, -rest
66 Supported formats: -latex, -brief, -rest
67 """
67 """
68
68
69 mode = ''
69 mode = ''
70 try:
70 try:
71 mode = parameter_s.split()[0][1:]
71 mode = parameter_s.split()[0][1:]
72 if mode == 'rest':
72 if mode == 'rest':
73 rest_docs = []
73 rest_docs = []
74 except:
74 except:
75 pass
75 pass
76
76
77 magic_docs = []
77 magic_docs = []
78 escapes = dict(line=ESC_MAGIC, cell=ESC_MAGIC*2)
78 escapes = dict(line=ESC_MAGIC, cell=ESC_MAGIC*2)
79 magics = self.shell.magics_manager.magics
79 magics = self.shell.magics_manager.magics
80
80
81 for mtype in ('line', 'cell'):
81 for mtype in ('line', 'cell'):
82 escape = escapes[mtype]
82 escape = escapes[mtype]
83 for fname, fn in magics[mtype].iteritems():
83 for fname, fn in magics[mtype].iteritems():
84
84
85 if mode == 'brief':
85 if mode == 'brief':
86 # only first line
86 # only first line
87 if fn.__doc__:
87 if fn.__doc__:
88 fndoc = fn.__doc__.split('\n',1)[0]
88 fndoc = fn.__doc__.split('\n',1)[0]
89 else:
89 else:
90 fndoc = 'No documentation'
90 fndoc = 'No documentation'
91 else:
91 else:
92 if fn.__doc__:
92 if fn.__doc__:
93 fndoc = fn.__doc__.rstrip()
93 fndoc = fn.__doc__.rstrip()
94 else:
94 else:
95 fndoc = 'No documentation'
95 fndoc = 'No documentation'
96
96
97 if mode == 'rest':
97 if mode == 'rest':
98 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %
98 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %
99 (escape, fname, fndoc))
99 (escape, fname, fndoc))
100 else:
100 else:
101 magic_docs.append('%s%s:\n\t%s\n' %
101 magic_docs.append('%s%s:\n\t%s\n' %
102 (escape, fname, fndoc))
102 (escape, fname, fndoc))
103
103
104 magic_docs = ''.join(magic_docs)
104 magic_docs = ''.join(magic_docs)
105
105
106 if mode == 'rest':
106 if mode == 'rest':
107 return "".join(rest_docs)
107 return "".join(rest_docs)
108
108
109 if mode == 'latex':
109 if mode == 'latex':
110 print(self.format_latex(magic_docs))
110 print(self.format_latex(magic_docs))
111 return
111 return
112 else:
112 else:
113 magic_docs = format_screen(magic_docs)
113 magic_docs = format_screen(magic_docs)
114 if mode == 'brief':
114 if mode == 'brief':
115 return magic_docs
115 return magic_docs
116
116
117 out = ["""
117 out = ["""
118 IPython's 'magic' functions
118 IPython's 'magic' functions
119 ===========================
119 ===========================
120
120
121 The magic function system provides a series of functions which allow you to
121 The magic function system provides a series of functions which allow you to
122 control the behavior of IPython itself, plus a lot of system-type
122 control the behavior of IPython itself, plus a lot of system-type
123 features. All these functions are prefixed with a % character, but parameters
123 features. All these functions are prefixed with a % character, but parameters
124 are given without parentheses or quotes.
124 are given without parentheses or quotes.
125
125
126 NOTE: If you have 'automagic' enabled (via the command line option or with the
126 NOTE: If you have 'automagic' enabled (via the command line option or with the
127 %automagic function), you don't need to type in the % explicitly. By default,
127 %automagic function), you don't need to type in the % explicitly. By default,
128 IPython ships with automagic on, so you should only rarely need the % escape.
128 IPython ships with automagic on, so you should only rarely need the % escape.
129
129
130 Example: typing '%cd mydir' (without the quotes) changes you working directory
130 Example: typing '%cd mydir' (without the quotes) changes you working directory
131 to 'mydir', if it exists.
131 to 'mydir', if it exists.
132
132
133 For a list of the available magic functions, use %lsmagic. For a description
133 For a list of the available magic functions, use %lsmagic. For a description
134 of any of them, type %magic_name?, e.g. '%cd?'.
134 of any of them, type %magic_name?, e.g. '%cd?'.
135
135
136 Currently the magic system has the following functions:""",
136 Currently the magic system has the following functions:""",
137 magic_docs,
137 magic_docs,
138 "Summary of magic functions (from %slsmagic):",
138 "Summary of magic functions (from %slsmagic):",
139 self._lsmagic(),
139 self._lsmagic(),
140 ]
140 ]
141 page.page('\n'.join(out))
141 page.page('\n'.join(out))
142
142
143
143
144 @line_magic
144 @line_magic
145 def page(self, parameter_s=''):
145 def page(self, parameter_s=''):
146 """Pretty print the object and display it through a pager.
146 """Pretty print the object and display it through a pager.
147
147
148 %page [options] OBJECT
148 %page [options] OBJECT
149
149
150 If no object is given, use _ (last output).
150 If no object is given, use _ (last output).
151
151
152 Options:
152 Options:
153
153
154 -r: page str(object), don't pretty-print it."""
154 -r: page str(object), don't pretty-print it."""
155
155
156 # After a function contributed by Olivier Aubert, slightly modified.
156 # After a function contributed by Olivier Aubert, slightly modified.
157
157
158 # Process options/args
158 # Process options/args
159 opts, args = self.parse_options(parameter_s, 'r')
159 opts, args = self.parse_options(parameter_s, 'r')
160 raw = 'r' in opts
160 raw = 'r' in opts
161
161
162 oname = args and args or '_'
162 oname = args and args or '_'
163 info = self._ofind(oname)
163 info = self._ofind(oname)
164 if info['found']:
164 if info['found']:
165 txt = (raw and str or pformat)( info['obj'] )
165 txt = (raw and str or pformat)( info['obj'] )
166 page.page(txt)
166 page.page(txt)
167 else:
167 else:
168 print('Object `%s` not found' % oname)
168 print('Object `%s` not found' % oname)
169
169
170 @line_magic
170 @line_magic
171 def profile(self, parameter_s=''):
171 def profile(self, parameter_s=''):
172 """Print your currently active IPython profile."""
172 """Print your currently active IPython profile."""
173 from IPython.core.application import BaseIPythonApplication
173 from IPython.core.application import BaseIPythonApplication
174 if BaseIPythonApplication.initialized():
174 if BaseIPythonApplication.initialized():
175 print(BaseIPythonApplication.instance().profile)
175 print(BaseIPythonApplication.instance().profile)
176 else:
176 else:
177 error("profile is an application-level value, but you don't appear to be in an IPython application")
177 error("profile is an application-level value, but you don't appear to be in an IPython application")
178
178
179 @line_magic
179 @line_magic
180 def pprint(self, parameter_s=''):
180 def pprint(self, parameter_s=''):
181 """Toggle pretty printing on/off."""
181 """Toggle pretty printing on/off."""
182 ptformatter = self.shell.display_formatter.formatters['text/plain']
182 ptformatter = self.shell.display_formatter.formatters['text/plain']
183 ptformatter.pprint = bool(1 - ptformatter.pprint)
183 ptformatter.pprint = bool(1 - ptformatter.pprint)
184 print('Pretty printing has been turned',
184 print('Pretty printing has been turned',
185 ['OFF','ON'][ptformatter.pprint])
185 ['OFF','ON'][ptformatter.pprint])
186
186
187 @line_magic
187 @line_magic
188 def colors(self, parameter_s=''):
188 def colors(self, parameter_s=''):
189 """Switch color scheme for prompts, info system and exception handlers.
189 """Switch color scheme for prompts, info system and exception handlers.
190
190
191 Currently implemented schemes: NoColor, Linux, LightBG.
191 Currently implemented schemes: NoColor, Linux, LightBG.
192
192
193 Color scheme names are not case-sensitive.
193 Color scheme names are not case-sensitive.
194
194
195 Examples
195 Examples
196 --------
196 --------
197 To get a plain black and white terminal::
197 To get a plain black and white terminal::
198
198
199 %colors nocolor
199 %colors nocolor
200 """
200 """
201 def color_switch_err(name):
201 def color_switch_err(name):
202 warn('Error changing %s color schemes.\n%s' %
202 warn('Error changing %s color schemes.\n%s' %
203 (name, sys.exc_info()[1]))
203 (name, sys.exc_info()[1]))
204
204
205
205
206 new_scheme = parameter_s.strip()
206 new_scheme = parameter_s.strip()
207 if not new_scheme:
207 if not new_scheme:
208 raise UsageError(
208 raise UsageError(
209 "%colors: you must specify a color scheme. See '%colors?'")
209 "%colors: you must specify a color scheme. See '%colors?'")
210 return
210 return
211 # local shortcut
211 # local shortcut
212 shell = self.shell
212 shell = self.shell
213
213
214 import IPython.utils.rlineimpl as readline
214 import IPython.utils.rlineimpl as readline
215
215
216 if not shell.colors_force and \
216 if not shell.colors_force and \
217 not readline.have_readline and sys.platform == "win32":
217 not readline.have_readline and sys.platform == "win32":
218 msg = """\
218 msg = """\
219 Proper color support under MS Windows requires the pyreadline library.
219 Proper color support under MS Windows requires the pyreadline library.
220 You can find it at:
220 You can find it at:
221 http://ipython.org/pyreadline.html
221 http://ipython.org/pyreadline.html
222 Gary's readline needs the ctypes module, from:
222 Gary's readline needs the ctypes module, from:
223 http://starship.python.net/crew/theller/ctypes
223 http://starship.python.net/crew/theller/ctypes
224 (Note that ctypes is already part of Python versions 2.5 and newer).
224 (Note that ctypes is already part of Python versions 2.5 and newer).
225
225
226 Defaulting color scheme to 'NoColor'"""
226 Defaulting color scheme to 'NoColor'"""
227 new_scheme = 'NoColor'
227 new_scheme = 'NoColor'
228 warn(msg)
228 warn(msg)
229
229
230 # readline option is 0
230 # readline option is 0
231 if not shell.colors_force and not shell.has_readline:
231 if not shell.colors_force and not shell.has_readline:
232 new_scheme = 'NoColor'
232 new_scheme = 'NoColor'
233
233
234 # Set prompt colors
234 # Set prompt colors
235 try:
235 try:
236 shell.prompt_manager.color_scheme = new_scheme
236 shell.prompt_manager.color_scheme = new_scheme
237 except:
237 except:
238 color_switch_err('prompt')
238 color_switch_err('prompt')
239 else:
239 else:
240 shell.colors = \
240 shell.colors = \
241 shell.prompt_manager.color_scheme_table.active_scheme_name
241 shell.prompt_manager.color_scheme_table.active_scheme_name
242 # Set exception colors
242 # Set exception colors
243 try:
243 try:
244 shell.InteractiveTB.set_colors(scheme = new_scheme)
244 shell.InteractiveTB.set_colors(scheme = new_scheme)
245 shell.SyntaxTB.set_colors(scheme = new_scheme)
245 shell.SyntaxTB.set_colors(scheme = new_scheme)
246 except:
246 except:
247 color_switch_err('exception')
247 color_switch_err('exception')
248
248
249 # Set info (for 'object?') colors
249 # Set info (for 'object?') colors
250 if shell.color_info:
250 if shell.color_info:
251 try:
251 try:
252 shell.inspector.set_active_scheme(new_scheme)
252 shell.inspector.set_active_scheme(new_scheme)
253 except:
253 except:
254 color_switch_err('object inspector')
254 color_switch_err('object inspector')
255 else:
255 else:
256 shell.inspector.set_active_scheme('NoColor')
256 shell.inspector.set_active_scheme('NoColor')
257
257
258 @line_magic
258 @line_magic
259 def xmode(self, parameter_s=''):
259 def xmode(self, parameter_s=''):
260 """Switch modes for the exception handlers.
260 """Switch modes for the exception handlers.
261
261
262 Valid modes: Plain, Context and Verbose.
262 Valid modes: Plain, Context and Verbose.
263
263
264 If called without arguments, acts as a toggle."""
264 If called without arguments, acts as a toggle."""
265
265
266 def xmode_switch_err(name):
266 def xmode_switch_err(name):
267 warn('Error changing %s exception modes.\n%s' %
267 warn('Error changing %s exception modes.\n%s' %
268 (name,sys.exc_info()[1]))
268 (name,sys.exc_info()[1]))
269
269
270 shell = self.shell
270 shell = self.shell
271 new_mode = parameter_s.strip().capitalize()
271 new_mode = parameter_s.strip().capitalize()
272 try:
272 try:
273 shell.InteractiveTB.set_mode(mode=new_mode)
273 shell.InteractiveTB.set_mode(mode=new_mode)
274 print('Exception reporting mode:',shell.InteractiveTB.mode)
274 print('Exception reporting mode:',shell.InteractiveTB.mode)
275 except:
275 except:
276 xmode_switch_err('user')
276 xmode_switch_err('user')
277
277
278 @line_magic
278 @line_magic
279 def quickref(self,arg):
279 def quickref(self,arg):
280 """ Show a quick reference sheet """
280 """ Show a quick reference sheet """
281 from IPython.core.usage import quick_reference
281 from IPython.core.usage import quick_reference
282 qr = quick_reference + self.magic('-brief')
282 qr = quick_reference + self.magic('-brief')
283 page.page(qr)
283 page.page(qr)
284
284
285 @line_magic
285 @line_magic
286 def doctest_mode(self, parameter_s=''):
286 def doctest_mode(self, parameter_s=''):
287 """Toggle doctest mode on and off.
287 """Toggle doctest mode on and off.
288
288
289 This mode is intended to make IPython behave as much as possible like a
289 This mode is intended to make IPython behave as much as possible like a
290 plain Python shell, from the perspective of how its prompts, exceptions
290 plain Python shell, from the perspective of how its prompts, exceptions
291 and output look. This makes it easy to copy and paste parts of a
291 and output look. This makes it easy to copy and paste parts of a
292 session into doctests. It does so by:
292 session into doctests. It does so by:
293
293
294 - Changing the prompts to the classic ``>>>`` ones.
294 - Changing the prompts to the classic ``>>>`` ones.
295 - Changing the exception reporting mode to 'Plain'.
295 - Changing the exception reporting mode to 'Plain'.
296 - Disabling pretty-printing of output.
296 - Disabling pretty-printing of output.
297
297
298 Note that IPython also supports the pasting of code snippets that have
298 Note that IPython also supports the pasting of code snippets that have
299 leading '>>>' and '...' prompts in them. This means that you can paste
299 leading '>>>' and '...' prompts in them. This means that you can paste
300 doctests from files or docstrings (even if they have leading
300 doctests from files or docstrings (even if they have leading
301 whitespace), and the code will execute correctly. You can then use
301 whitespace), and the code will execute correctly. You can then use
302 '%history -t' to see the translated history; this will give you the
302 '%history -t' to see the translated history; this will give you the
303 input after removal of all the leading prompts and whitespace, which
303 input after removal of all the leading prompts and whitespace, which
304 can be pasted back into an editor.
304 can be pasted back into an editor.
305
305
306 With these features, you can switch into this mode easily whenever you
306 With these features, you can switch into this mode easily whenever you
307 need to do testing and changes to doctests, without having to leave
307 need to do testing and changes to doctests, without having to leave
308 your existing IPython session.
308 your existing IPython session.
309 """
309 """
310
310
311 # Shorthands
311 # Shorthands
312 shell = self.shell
312 shell = self.shell
313 pm = shell.prompt_manager
313 pm = shell.prompt_manager
314 meta = shell.meta
314 meta = shell.meta
315 disp_formatter = self.shell.display_formatter
315 disp_formatter = self.shell.display_formatter
316 ptformatter = disp_formatter.formatters['text/plain']
316 ptformatter = disp_formatter.formatters['text/plain']
317 # dstore is a data store kept in the instance metadata bag to track any
317 # dstore is a data store kept in the instance metadata bag to track any
318 # changes we make, so we can undo them later.
318 # changes we make, so we can undo them later.
319 dstore = meta.setdefault('doctest_mode',Struct())
319 dstore = meta.setdefault('doctest_mode',Struct())
320 save_dstore = dstore.setdefault
320 save_dstore = dstore.setdefault
321
321
322 # save a few values we'll need to recover later
322 # save a few values we'll need to recover later
323 mode = save_dstore('mode',False)
323 mode = save_dstore('mode',False)
324 save_dstore('rc_pprint',ptformatter.pprint)
324 save_dstore('rc_pprint',ptformatter.pprint)
325 save_dstore('xmode',shell.InteractiveTB.mode)
325 save_dstore('xmode',shell.InteractiveTB.mode)
326 save_dstore('rc_separate_out',shell.separate_out)
326 save_dstore('rc_separate_out',shell.separate_out)
327 save_dstore('rc_separate_out2',shell.separate_out2)
327 save_dstore('rc_separate_out2',shell.separate_out2)
328 save_dstore('rc_prompts_pad_left',pm.justify)
328 save_dstore('rc_prompts_pad_left',pm.justify)
329 save_dstore('rc_separate_in',shell.separate_in)
329 save_dstore('rc_separate_in',shell.separate_in)
330 save_dstore('rc_plain_text_only',disp_formatter.plain_text_only)
330 save_dstore('rc_plain_text_only',disp_formatter.plain_text_only)
331 save_dstore('prompt_templates',(pm.in_template, pm.in2_template, pm.out_template))
331 save_dstore('prompt_templates',(pm.in_template, pm.in2_template, pm.out_template))
332
332
333 if mode == False:
333 if mode == False:
334 # turn on
334 # turn on
335 pm.in_template = '>>> '
335 pm.in_template = '>>> '
336 pm.in2_template = '... '
336 pm.in2_template = '... '
337 pm.out_template = ''
337 pm.out_template = ''
338
338
339 # Prompt separators like plain python
339 # Prompt separators like plain python
340 shell.separate_in = ''
340 shell.separate_in = ''
341 shell.separate_out = ''
341 shell.separate_out = ''
342 shell.separate_out2 = ''
342 shell.separate_out2 = ''
343
343
344 pm.justify = False
344 pm.justify = False
345
345
346 ptformatter.pprint = False
346 ptformatter.pprint = False
347 disp_formatter.plain_text_only = True
347 disp_formatter.plain_text_only = True
348
348
349 shell.magic('xmode Plain')
349 shell.magic('xmode Plain')
350 else:
350 else:
351 # turn off
351 # turn off
352 pm.in_template, pm.in2_template, pm.out_template = dstore.prompt_templates
352 pm.in_template, pm.in2_template, pm.out_template = dstore.prompt_templates
353
353
354 shell.separate_in = dstore.rc_separate_in
354 shell.separate_in = dstore.rc_separate_in
355
355
356 shell.separate_out = dstore.rc_separate_out
356 shell.separate_out = dstore.rc_separate_out
357 shell.separate_out2 = dstore.rc_separate_out2
357 shell.separate_out2 = dstore.rc_separate_out2
358
358
359 pm.justify = dstore.rc_prompts_pad_left
359 pm.justify = dstore.rc_prompts_pad_left
360
360
361 ptformatter.pprint = dstore.rc_pprint
361 ptformatter.pprint = dstore.rc_pprint
362 disp_formatter.plain_text_only = dstore.rc_plain_text_only
362 disp_formatter.plain_text_only = dstore.rc_plain_text_only
363
363
364 shell.magic('xmode ' + dstore.xmode)
364 shell.magic('xmode ' + dstore.xmode)
365
365
366 # Store new mode and inform
366 # Store new mode and inform
367 dstore.mode = bool(1-int(mode))
367 dstore.mode = bool(1-int(mode))
368 mode_label = ['OFF','ON'][dstore.mode]
368 mode_label = ['OFF','ON'][dstore.mode]
369 print('Doctest mode is:', mode_label)
369 print('Doctest mode is:', mode_label)
370
370
371 @line_magic
371 @line_magic
372 def gui(self, parameter_s=''):
372 def gui(self, parameter_s=''):
373 """Enable or disable IPython GUI event loop integration.
373 """Enable or disable IPython GUI event loop integration.
374
374
375 %gui [GUINAME]
375 %gui [GUINAME]
376
376
377 This magic replaces IPython's threaded shells that were activated
377 This magic replaces IPython's threaded shells that were activated
378 using the (pylab/wthread/etc.) command line flags. GUI toolkits
378 using the (pylab/wthread/etc.) command line flags. GUI toolkits
379 can now be enabled at runtime and keyboard
379 can now be enabled at runtime and keyboard
380 interrupts should work without any problems. The following toolkits
380 interrupts should work without any problems. The following toolkits
381 are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX)::
381 are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX)::
382
382
383 %gui wx # enable wxPython event loop integration
383 %gui wx # enable wxPython event loop integration
384 %gui qt4|qt # enable PyQt4 event loop integration
384 %gui qt4|qt # enable PyQt4 event loop integration
385 %gui gtk # enable PyGTK event loop integration
385 %gui gtk # enable PyGTK event loop integration
386 %gui gtk3 # enable Gtk3 event loop integration
386 %gui gtk3 # enable Gtk3 event loop integration
387 %gui tk # enable Tk event loop integration
387 %gui tk # enable Tk event loop integration
388 %gui OSX # enable Cocoa event loop integration
388 %gui OSX # enable Cocoa event loop integration
389 # (requires %matplotlib 1.1)
389 # (requires %matplotlib 1.1)
390 %gui # disable all event loop integration
390 %gui # disable all event loop integration
391
391
392 WARNING: after any of these has been called you can simply create
392 WARNING: after any of these has been called you can simply create
393 an application object, but DO NOT start the event loop yourself, as
393 an application object, but DO NOT start the event loop yourself, as
394 we have already handled that.
394 we have already handled that.
395 """
395 """
396 opts, arg = self.parse_options(parameter_s, '')
396 opts, arg = self.parse_options(parameter_s, '')
397 if arg=='': arg = None
397 if arg=='': arg = None
398 try:
398 try:
399 return self.enable_gui(arg)
399 return self.enable_gui(arg)
400 except Exception as e:
400 except Exception as e:
401 # print simple error message, rather than traceback if we can't
401 # print simple error message, rather than traceback if we can't
402 # hook up the GUI
402 # hook up the GUI
403 error(str(e))
403 error(str(e))
404
404
405 @skip_doctest
405 @skip_doctest
406 @line_magic
406 @line_magic
407 def precision(self, s=''):
407 def precision(self, s=''):
408 """Set floating point precision for pretty printing.
408 """Set floating point precision for pretty printing.
409
409
410 Can set either integer precision or a format string.
410 Can set either integer precision or a format string.
411
411
412 If numpy has been imported and precision is an int,
412 If numpy has been imported and precision is an int,
413 numpy display precision will also be set, via ``numpy.set_printoptions``.
413 numpy display precision will also be set, via ``numpy.set_printoptions``.
414
414
415 If no argument is given, defaults will be restored.
415 If no argument is given, defaults will be restored.
416
416
417 Examples
417 Examples
418 --------
418 --------
419 ::
419 ::
420
420
421 In [1]: from math import pi
421 In [1]: from math import pi
422
422
423 In [2]: %precision 3
423 In [2]: %precision 3
424 Out[2]: u'%.3f'
424 Out[2]: u'%.3f'
425
425
426 In [3]: pi
426 In [3]: pi
427 Out[3]: 3.142
427 Out[3]: 3.142
428
428
429 In [4]: %precision %i
429 In [4]: %precision %i
430 Out[4]: u'%i'
430 Out[4]: u'%i'
431
431
432 In [5]: pi
432 In [5]: pi
433 Out[5]: 3
433 Out[5]: 3
434
434
435 In [6]: %precision %e
435 In [6]: %precision %e
436 Out[6]: u'%e'
436 Out[6]: u'%e'
437
437
438 In [7]: pi**10
438 In [7]: pi**10
439 Out[7]: 9.364805e+04
439 Out[7]: 9.364805e+04
440
440
441 In [8]: %precision
441 In [8]: %precision
442 Out[8]: u'%r'
442 Out[8]: u'%r'
443
443
444 In [9]: pi**10
444 In [9]: pi**10
445 Out[9]: 93648.047476082982
445 Out[9]: 93648.047476082982
446 """
446 """
447 ptformatter = self.shell.display_formatter.formatters['text/plain']
447 ptformatter = self.shell.display_formatter.formatters['text/plain']
448 ptformatter.float_precision = s
448 ptformatter.float_precision = s
449 return ptformatter.float_format
449 return ptformatter.float_format
450
450
451 @magic_arguments.magic_arguments()
451 @magic_arguments.magic_arguments()
452 @magic_arguments.argument(
452 @magic_arguments.argument(
453 '-e', '--export', action='store_true', default=False,
453 '-e', '--export', action='store_true', default=False,
454 help='Export IPython history as a notebook. The filename argument '
454 help='Export IPython history as a notebook. The filename argument '
455 'is used to specify the notebook name and format. For example '
455 'is used to specify the notebook name and format. For example '
456 'a filename of notebook.ipynb will result in a notebook name '
456 'a filename of notebook.ipynb will result in a notebook name '
457 'of "notebook" and a format of "xml". Likewise using a ".json" '
457 'of "notebook" and a format of "xml". Likewise using a ".json" '
458 'or ".py" file extension will write the notebook in the json '
458 'or ".py" file extension will write the notebook in the json '
459 'or py formats.'
459 'or py formats.'
460 )
460 )
461 @magic_arguments.argument(
461 @magic_arguments.argument(
462 '-f', '--format',
462 '-f', '--format',
463 help='Convert an existing IPython notebook to a new format. This option '
463 help='Convert an existing IPython notebook to a new format. This option '
464 'specifies the new format and can have the values: xml, json, py. '
464 'specifies the new format and can have the values: xml, json, py. '
465 'The target filename is chosen automatically based on the new '
465 'The target filename is chosen automatically based on the new '
466 'format. The filename argument gives the name of the source file.'
466 'format. The filename argument gives the name of the source file.'
467 )
467 )
468 @magic_arguments.argument(
468 @magic_arguments.argument(
469 'filename', type=unicode,
469 'filename', type=unicode,
470 help='Notebook name or filename'
470 help='Notebook name or filename'
471 )
471 )
472 @line_magic
472 @line_magic
473 def notebook(self, s):
473 def notebook(self, s):
474 """Export and convert IPython notebooks.
474 """Export and convert IPython notebooks.
475
475
476 This function can export the current IPython history to a notebook file
476 This function can export the current IPython history to a notebook file
477 or can convert an existing notebook file into a different format. For
477 or can convert an existing notebook file into a different format. For
478 example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
478 example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
479 To export the history to "foo.py" do "%notebook -e foo.py". To convert
479 To export the history to "foo.py" do "%notebook -e foo.py". To convert
480 "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
480 "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
481 formats include (json/ipynb, py).
481 formats include (json/ipynb, py).
482 """
482 """
483 args = magic_arguments.parse_argstring(self.notebook, s)
483 args = magic_arguments.parse_argstring(self.notebook, s)
484
484
485 from IPython.nbformat import current
485 from IPython.nbformat import current
486 args.filename = unquote_filename(args.filename)
486 args.filename = unquote_filename(args.filename)
487 if args.export:
487 if args.export:
488 fname, name, format = current.parse_filename(args.filename)
488 fname, name, format = current.parse_filename(args.filename)
489 cells = []
489 cells = []
490 hist = list(self.shell.history_manager.get_range())
490 hist = list(self.shell.history_manager.get_range())
491 for session, prompt_number, input in hist[:-1]:
491 for session, prompt_number, input in hist[:-1]:
492 cells.append(current.new_code_cell(prompt_number=prompt_number,
492 cells.append(current.new_code_cell(prompt_number=prompt_number,
493 input=input))
493 input=input))
494 worksheet = current.new_worksheet(cells=cells)
494 worksheet = current.new_worksheet(cells=cells)
495 nb = current.new_notebook(name=name,worksheets=[worksheet])
495 nb = current.new_notebook(name=name,worksheets=[worksheet])
496 with io.open(fname, 'w', encoding='utf-8') as f:
496 with io.open(fname, 'w', encoding='utf-8') as f:
497 current.write(nb, f, format);
497 current.write(nb, f, format);
498 elif args.format is not None:
498 elif args.format is not None:
499 old_fname, old_name, old_format = current.parse_filename(args.filename)
499 old_fname, old_name, old_format = current.parse_filename(args.filename)
500 new_format = args.format
500 new_format = args.format
501 if new_format == u'xml':
501 if new_format == u'xml':
502 raise ValueError('Notebooks cannot be written as xml.')
502 raise ValueError('Notebooks cannot be written as xml.')
503 elif new_format == u'ipynb' or new_format == u'json':
503 elif new_format == u'ipynb' or new_format == u'json':
504 new_fname = old_name + u'.ipynb'
504 new_fname = old_name + u'.ipynb'
505 new_format = u'json'
505 new_format = u'json'
506 elif new_format == u'py':
506 elif new_format == u'py':
507 new_fname = old_name + u'.py'
507 new_fname = old_name + u'.py'
508 else:
508 else:
509 raise ValueError('Invalid notebook format: %s' % new_format)
509 raise ValueError('Invalid notebook format: %s' % new_format)
510 with io.open(old_fname, 'r', encoding='utf-8') as f:
510 with io.open(old_fname, 'r', encoding='utf-8') as f:
511 nb = current.read(f, old_format)
511 nb = current.read(f, old_format)
512 with io.open(new_fname, 'w', encoding='utf-8') as f:
512 with io.open(new_fname, 'w', encoding='utf-8') as f:
513 current.write(nb, f, new_format)
513 current.write(nb, f, new_format)
@@ -1,478 +1,478 b''
1 """Implementation of code management magic functions.
1 """Implementation of code management magic functions.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Stdlib
15 # Stdlib
16 import inspect
16 import inspect
17 import io
17 import io
18 import json
18 import json
19 import os
19 import os
20 import sys
20 import sys
21 from urllib2 import urlopen
21 from urllib2 import urlopen
22
22
23 # Our own packages
23 # Our own packages
24 from IPython.core.error import TryNext
24 from IPython.core.error import TryNext
25 from IPython.core.macro import Macro
25 from IPython.core.macro import Macro
26 from IPython.core.magic import Magics, register_magics, line_magic
26 from IPython.core.magic import Magics, magics_class, line_magic
27 from IPython.testing.skipdoctest import skip_doctest
27 from IPython.testing.skipdoctest import skip_doctest
28 from IPython.utils import openpy
28 from IPython.utils import openpy
29 from IPython.utils import py3compat
29 from IPython.utils import py3compat
30 from IPython.utils.io import file_read
30 from IPython.utils.io import file_read
31 from IPython.utils.path import get_py_filename, unquote_filename
31 from IPython.utils.path import get_py_filename, unquote_filename
32 from IPython.utils.warn import warn
32 from IPython.utils.warn import warn
33
33
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35 # Magic implementation classes
35 # Magic implementation classes
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37
37
38 # Used for exception handling in magic_edit
38 # Used for exception handling in magic_edit
39 class MacroToEdit(ValueError): pass
39 class MacroToEdit(ValueError): pass
40
40
41
41
42 @register_magics
42 @magics_class
43 class CodeMagics(Magics):
43 class CodeMagics(Magics):
44 """Magics related to code management (loading, saving, editing, ...)."""
44 """Magics related to code management (loading, saving, editing, ...)."""
45
45
46 @line_magic
46 @line_magic
47 def save(self, parameter_s=''):
47 def save(self, parameter_s=''):
48 """Save a set of lines or a macro to a given filename.
48 """Save a set of lines or a macro to a given filename.
49
49
50 Usage:\\
50 Usage:\\
51 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
51 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
52
52
53 Options:
53 Options:
54
54
55 -r: use 'raw' input. By default, the 'processed' history is used,
55 -r: use 'raw' input. By default, the 'processed' history is used,
56 so that magics are loaded in their transformed version to valid
56 so that magics are loaded in their transformed version to valid
57 Python. If this option is given, the raw input as typed as the
57 Python. If this option is given, the raw input as typed as the
58 command line is used instead.
58 command line is used instead.
59
59
60 This function uses the same syntax as %history for input ranges,
60 This function uses the same syntax as %history for input ranges,
61 then saves the lines to the filename you specify.
61 then saves the lines to the filename you specify.
62
62
63 It adds a '.py' extension to the file if you don't do so yourself, and
63 It adds a '.py' extension to the file if you don't do so yourself, and
64 it asks for confirmation before overwriting existing files."""
64 it asks for confirmation before overwriting existing files."""
65
65
66 opts,args = self.parse_options(parameter_s,'r',mode='list')
66 opts,args = self.parse_options(parameter_s,'r',mode='list')
67 fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
67 fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
68 if not fname.endswith('.py'):
68 if not fname.endswith('.py'):
69 fname += '.py'
69 fname += '.py'
70 if os.path.isfile(fname):
70 if os.path.isfile(fname):
71 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
71 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
72 if ans.lower() not in ['y','yes']:
72 if ans.lower() not in ['y','yes']:
73 print 'Operation cancelled.'
73 print 'Operation cancelled.'
74 return
74 return
75 try:
75 try:
76 cmds = self.shell.find_user_code(codefrom, 'r' in opts)
76 cmds = self.shell.find_user_code(codefrom, 'r' in opts)
77 except (TypeError, ValueError) as e:
77 except (TypeError, ValueError) as e:
78 print e.args[0]
78 print e.args[0]
79 return
79 return
80 with io.open(fname,'w', encoding="utf-8") as f:
80 with io.open(fname,'w', encoding="utf-8") as f:
81 f.write(u"# coding: utf-8\n")
81 f.write(u"# coding: utf-8\n")
82 f.write(py3compat.cast_unicode(cmds))
82 f.write(py3compat.cast_unicode(cmds))
83 print 'The following commands were written to file `%s`:' % fname
83 print 'The following commands were written to file `%s`:' % fname
84 print cmds
84 print cmds
85
85
86 @line_magic
86 @line_magic
87 def pastebin(self, parameter_s=''):
87 def pastebin(self, parameter_s=''):
88 """Upload code to Github's Gist paste bin, returning the URL.
88 """Upload code to Github's Gist paste bin, returning the URL.
89
89
90 Usage:\\
90 Usage:\\
91 %pastebin [-d "Custom description"] 1-7
91 %pastebin [-d "Custom description"] 1-7
92
92
93 The argument can be an input history range, a filename, or the name of a
93 The argument can be an input history range, a filename, or the name of a
94 string or macro.
94 string or macro.
95
95
96 Options:
96 Options:
97
97
98 -d: Pass a custom description for the gist. The default will say
98 -d: Pass a custom description for the gist. The default will say
99 "Pasted from IPython".
99 "Pasted from IPython".
100 """
100 """
101 opts, args = self.parse_options(parameter_s, 'd:')
101 opts, args = self.parse_options(parameter_s, 'd:')
102
102
103 try:
103 try:
104 code = self.shell.find_user_code(args)
104 code = self.shell.find_user_code(args)
105 except (ValueError, TypeError) as e:
105 except (ValueError, TypeError) as e:
106 print e.args[0]
106 print e.args[0]
107 return
107 return
108
108
109 post_data = json.dumps({
109 post_data = json.dumps({
110 "description": opts.get('d', "Pasted from IPython"),
110 "description": opts.get('d', "Pasted from IPython"),
111 "public": True,
111 "public": True,
112 "files": {
112 "files": {
113 "file1.py": {
113 "file1.py": {
114 "content": code
114 "content": code
115 }
115 }
116 }
116 }
117 }).encode('utf-8')
117 }).encode('utf-8')
118
118
119 response = urlopen("https://api.github.com/gists", post_data)
119 response = urlopen("https://api.github.com/gists", post_data)
120 response_data = json.loads(response.read().decode('utf-8'))
120 response_data = json.loads(response.read().decode('utf-8'))
121 return response_data['html_url']
121 return response_data['html_url']
122
122
123 @line_magic
123 @line_magic
124 def loadpy(self, arg_s):
124 def loadpy(self, arg_s):
125 """Load a .py python script into the GUI console.
125 """Load a .py python script into the GUI console.
126
126
127 This magic command can either take a local filename or a url::
127 This magic command can either take a local filename or a url::
128
128
129 %loadpy myscript.py
129 %loadpy myscript.py
130 %loadpy http://www.example.com/myscript.py
130 %loadpy http://www.example.com/myscript.py
131 """
131 """
132 arg_s = unquote_filename(arg_s)
132 arg_s = unquote_filename(arg_s)
133 remote_url = arg_s.startswith(('http://', 'https://'))
133 remote_url = arg_s.startswith(('http://', 'https://'))
134 local_url = not remote_url
134 local_url = not remote_url
135 if local_url and not arg_s.endswith('.py'):
135 if local_url and not arg_s.endswith('.py'):
136 # Local files must be .py; for remote URLs it's possible that the
136 # Local files must be .py; for remote URLs it's possible that the
137 # fetch URL doesn't have a .py in it (many servers have an opaque
137 # fetch URL doesn't have a .py in it (many servers have an opaque
138 # URL, such as scipy-central.org).
138 # URL, such as scipy-central.org).
139 raise ValueError('%%loadpy only works with .py files: %s' % arg_s)
139 raise ValueError('%%loadpy only works with .py files: %s' % arg_s)
140
140
141 # openpy takes care of finding the source encoding (per PEP 263)
141 # openpy takes care of finding the source encoding (per PEP 263)
142 if remote_url:
142 if remote_url:
143 contents = openpy.read_py_url(arg_s, skip_encoding_cookie=True)
143 contents = openpy.read_py_url(arg_s, skip_encoding_cookie=True)
144 else:
144 else:
145 contents = openpy.read_py_file(arg_s, skip_encoding_cookie=True)
145 contents = openpy.read_py_file(arg_s, skip_encoding_cookie=True)
146
146
147 self.shell.set_next_input(contents)
147 self.shell.set_next_input(contents)
148
148
149 def _find_edit_target(self, args, opts, last_call):
149 def _find_edit_target(self, args, opts, last_call):
150 """Utility method used by magic_edit to find what to edit."""
150 """Utility method used by magic_edit to find what to edit."""
151
151
152 def make_filename(arg):
152 def make_filename(arg):
153 "Make a filename from the given args"
153 "Make a filename from the given args"
154 arg = unquote_filename(arg)
154 arg = unquote_filename(arg)
155 try:
155 try:
156 filename = get_py_filename(arg)
156 filename = get_py_filename(arg)
157 except IOError:
157 except IOError:
158 # If it ends with .py but doesn't already exist, assume we want
158 # If it ends with .py but doesn't already exist, assume we want
159 # a new file.
159 # a new file.
160 if arg.endswith('.py'):
160 if arg.endswith('.py'):
161 filename = arg
161 filename = arg
162 else:
162 else:
163 filename = None
163 filename = None
164 return filename
164 return filename
165
165
166 # Set a few locals from the options for convenience:
166 # Set a few locals from the options for convenience:
167 opts_prev = 'p' in opts
167 opts_prev = 'p' in opts
168 opts_raw = 'r' in opts
168 opts_raw = 'r' in opts
169
169
170 # custom exceptions
170 # custom exceptions
171 class DataIsObject(Exception): pass
171 class DataIsObject(Exception): pass
172
172
173 # Default line number value
173 # Default line number value
174 lineno = opts.get('n',None)
174 lineno = opts.get('n',None)
175
175
176 if opts_prev:
176 if opts_prev:
177 args = '_%s' % last_call[0]
177 args = '_%s' % last_call[0]
178 if not self.shell.user_ns.has_key(args):
178 if not self.shell.user_ns.has_key(args):
179 args = last_call[1]
179 args = last_call[1]
180
180
181 # use last_call to remember the state of the previous call, but don't
181 # use last_call to remember the state of the previous call, but don't
182 # let it be clobbered by successive '-p' calls.
182 # let it be clobbered by successive '-p' calls.
183 try:
183 try:
184 last_call[0] = self.shell.displayhook.prompt_count
184 last_call[0] = self.shell.displayhook.prompt_count
185 if not opts_prev:
185 if not opts_prev:
186 last_call[1] = args
186 last_call[1] = args
187 except:
187 except:
188 pass
188 pass
189
189
190 # by default this is done with temp files, except when the given
190 # by default this is done with temp files, except when the given
191 # arg is a filename
191 # arg is a filename
192 use_temp = True
192 use_temp = True
193
193
194 data = ''
194 data = ''
195
195
196 # First, see if the arguments should be a filename.
196 # First, see if the arguments should be a filename.
197 filename = make_filename(args)
197 filename = make_filename(args)
198 if filename:
198 if filename:
199 use_temp = False
199 use_temp = False
200 elif args:
200 elif args:
201 # Mode where user specifies ranges of lines, like in %macro.
201 # Mode where user specifies ranges of lines, like in %macro.
202 data = self.shell.extract_input_lines(args, opts_raw)
202 data = self.shell.extract_input_lines(args, opts_raw)
203 if not data:
203 if not data:
204 try:
204 try:
205 # Load the parameter given as a variable. If not a string,
205 # Load the parameter given as a variable. If not a string,
206 # process it as an object instead (below)
206 # process it as an object instead (below)
207
207
208 #print '*** args',args,'type',type(args) # dbg
208 #print '*** args',args,'type',type(args) # dbg
209 data = eval(args, self.shell.user_ns)
209 data = eval(args, self.shell.user_ns)
210 if not isinstance(data, basestring):
210 if not isinstance(data, basestring):
211 raise DataIsObject
211 raise DataIsObject
212
212
213 except (NameError,SyntaxError):
213 except (NameError,SyntaxError):
214 # given argument is not a variable, try as a filename
214 # given argument is not a variable, try as a filename
215 filename = make_filename(args)
215 filename = make_filename(args)
216 if filename is None:
216 if filename is None:
217 warn("Argument given (%s) can't be found as a variable "
217 warn("Argument given (%s) can't be found as a variable "
218 "or as a filename." % args)
218 "or as a filename." % args)
219 return
219 return
220 use_temp = False
220 use_temp = False
221
221
222 except DataIsObject:
222 except DataIsObject:
223 # macros have a special edit function
223 # macros have a special edit function
224 if isinstance(data, Macro):
224 if isinstance(data, Macro):
225 raise MacroToEdit(data)
225 raise MacroToEdit(data)
226
226
227 # For objects, try to edit the file where they are defined
227 # For objects, try to edit the file where they are defined
228 try:
228 try:
229 filename = inspect.getabsfile(data)
229 filename = inspect.getabsfile(data)
230 if 'fakemodule' in filename.lower() and \
230 if 'fakemodule' in filename.lower() and \
231 inspect.isclass(data):
231 inspect.isclass(data):
232 # class created by %edit? Try to find source
232 # class created by %edit? Try to find source
233 # by looking for method definitions instead, the
233 # by looking for method definitions instead, the
234 # __module__ in those classes is FakeModule.
234 # __module__ in those classes is FakeModule.
235 attrs = [getattr(data, aname) for aname in dir(data)]
235 attrs = [getattr(data, aname) for aname in dir(data)]
236 for attr in attrs:
236 for attr in attrs:
237 if not inspect.ismethod(attr):
237 if not inspect.ismethod(attr):
238 continue
238 continue
239 filename = inspect.getabsfile(attr)
239 filename = inspect.getabsfile(attr)
240 if filename and \
240 if filename and \
241 'fakemodule' not in filename.lower():
241 'fakemodule' not in filename.lower():
242 # change the attribute to be the edit
242 # change the attribute to be the edit
243 # target instead
243 # target instead
244 data = attr
244 data = attr
245 break
245 break
246
246
247 datafile = 1
247 datafile = 1
248 except TypeError:
248 except TypeError:
249 filename = make_filename(args)
249 filename = make_filename(args)
250 datafile = 1
250 datafile = 1
251 warn('Could not find file where `%s` is defined.\n'
251 warn('Could not find file where `%s` is defined.\n'
252 'Opening a file named `%s`' % (args,filename))
252 'Opening a file named `%s`' % (args,filename))
253 # Now, make sure we can actually read the source (if it was
253 # Now, make sure we can actually read the source (if it was
254 # in a temp file it's gone by now).
254 # in a temp file it's gone by now).
255 if datafile:
255 if datafile:
256 try:
256 try:
257 if lineno is None:
257 if lineno is None:
258 lineno = inspect.getsourcelines(data)[1]
258 lineno = inspect.getsourcelines(data)[1]
259 except IOError:
259 except IOError:
260 filename = make_filename(args)
260 filename = make_filename(args)
261 if filename is None:
261 if filename is None:
262 warn('The file `%s` where `%s` was defined cannot '
262 warn('The file `%s` where `%s` was defined cannot '
263 'be read.' % (filename,data))
263 'be read.' % (filename,data))
264 return
264 return
265 use_temp = False
265 use_temp = False
266
266
267 if use_temp:
267 if use_temp:
268 filename = self.shell.mktempfile(data)
268 filename = self.shell.mktempfile(data)
269 print 'IPython will make a temporary file named:',filename
269 print 'IPython will make a temporary file named:',filename
270
270
271 return filename, lineno, use_temp
271 return filename, lineno, use_temp
272
272
273 def _edit_macro(self,mname,macro):
273 def _edit_macro(self,mname,macro):
274 """open an editor with the macro data in a file"""
274 """open an editor with the macro data in a file"""
275 filename = self.shell.mktempfile(macro.value)
275 filename = self.shell.mktempfile(macro.value)
276 self.shell.hooks.editor(filename)
276 self.shell.hooks.editor(filename)
277
277
278 # and make a new macro object, to replace the old one
278 # and make a new macro object, to replace the old one
279 mfile = open(filename)
279 mfile = open(filename)
280 mvalue = mfile.read()
280 mvalue = mfile.read()
281 mfile.close()
281 mfile.close()
282 self.shell.user_ns[mname] = Macro(mvalue)
282 self.shell.user_ns[mname] = Macro(mvalue)
283
283
284 @line_magic
284 @line_magic
285 def ed(self, parameter_s=''):
285 def ed(self, parameter_s=''):
286 """Alias to %edit."""
286 """Alias to %edit."""
287 return self.edit(parameter_s)
287 return self.edit(parameter_s)
288
288
289 @skip_doctest
289 @skip_doctest
290 @line_magic
290 @line_magic
291 def edit(self, parameter_s='',last_call=['','']):
291 def edit(self, parameter_s='',last_call=['','']):
292 """Bring up an editor and execute the resulting code.
292 """Bring up an editor and execute the resulting code.
293
293
294 Usage:
294 Usage:
295 %edit [options] [args]
295 %edit [options] [args]
296
296
297 %edit runs IPython's editor hook. The default version of this hook is
297 %edit runs IPython's editor hook. The default version of this hook is
298 set to call the editor specified by your $EDITOR environment variable.
298 set to call the editor specified by your $EDITOR environment variable.
299 If this isn't found, it will default to vi under Linux/Unix and to
299 If this isn't found, it will default to vi under Linux/Unix and to
300 notepad under Windows. See the end of this docstring for how to change
300 notepad under Windows. See the end of this docstring for how to change
301 the editor hook.
301 the editor hook.
302
302
303 You can also set the value of this editor via the
303 You can also set the value of this editor via the
304 ``TerminalInteractiveShell.editor`` option in your configuration file.
304 ``TerminalInteractiveShell.editor`` option in your configuration file.
305 This is useful if you wish to use a different editor from your typical
305 This is useful if you wish to use a different editor from your typical
306 default with IPython (and for Windows users who typically don't set
306 default with IPython (and for Windows users who typically don't set
307 environment variables).
307 environment variables).
308
308
309 This command allows you to conveniently edit multi-line code right in
309 This command allows you to conveniently edit multi-line code right in
310 your IPython session.
310 your IPython session.
311
311
312 If called without arguments, %edit opens up an empty editor with a
312 If called without arguments, %edit opens up an empty editor with a
313 temporary file and will execute the contents of this file when you
313 temporary file and will execute the contents of this file when you
314 close it (don't forget to save it!).
314 close it (don't forget to save it!).
315
315
316
316
317 Options:
317 Options:
318
318
319 -n <number>: open the editor at a specified line number. By default,
319 -n <number>: open the editor at a specified line number. By default,
320 the IPython editor hook uses the unix syntax 'editor +N filename', but
320 the IPython editor hook uses the unix syntax 'editor +N filename', but
321 you can configure this by providing your own modified hook if your
321 you can configure this by providing your own modified hook if your
322 favorite editor supports line-number specifications with a different
322 favorite editor supports line-number specifications with a different
323 syntax.
323 syntax.
324
324
325 -p: this will call the editor with the same data as the previous time
325 -p: this will call the editor with the same data as the previous time
326 it was used, regardless of how long ago (in your current session) it
326 it was used, regardless of how long ago (in your current session) it
327 was.
327 was.
328
328
329 -r: use 'raw' input. This option only applies to input taken from the
329 -r: use 'raw' input. This option only applies to input taken from the
330 user's history. By default, the 'processed' history is used, so that
330 user's history. By default, the 'processed' history is used, so that
331 magics are loaded in their transformed version to valid Python. If
331 magics are loaded in their transformed version to valid Python. If
332 this option is given, the raw input as typed as the command line is
332 this option is given, the raw input as typed as the command line is
333 used instead. When you exit the editor, it will be executed by
333 used instead. When you exit the editor, it will be executed by
334 IPython's own processor.
334 IPython's own processor.
335
335
336 -x: do not execute the edited code immediately upon exit. This is
336 -x: do not execute the edited code immediately upon exit. This is
337 mainly useful if you are editing programs which need to be called with
337 mainly useful if you are editing programs which need to be called with
338 command line arguments, which you can then do using %run.
338 command line arguments, which you can then do using %run.
339
339
340
340
341 Arguments:
341 Arguments:
342
342
343 If arguments are given, the following possibilities exist:
343 If arguments are given, the following possibilities exist:
344
344
345 - If the argument is a filename, IPython will load that into the
345 - If the argument is a filename, IPython will load that into the
346 editor. It will execute its contents with execfile() when you exit,
346 editor. It will execute its contents with execfile() when you exit,
347 loading any code in the file into your interactive namespace.
347 loading any code in the file into your interactive namespace.
348
348
349 - The arguments are ranges of input history, e.g. "7 ~1/4-6".
349 - The arguments are ranges of input history, e.g. "7 ~1/4-6".
350 The syntax is the same as in the %history magic.
350 The syntax is the same as in the %history magic.
351
351
352 - If the argument is a string variable, its contents are loaded
352 - If the argument is a string variable, its contents are loaded
353 into the editor. You can thus edit any string which contains
353 into the editor. You can thus edit any string which contains
354 python code (including the result of previous edits).
354 python code (including the result of previous edits).
355
355
356 - If the argument is the name of an object (other than a string),
356 - If the argument is the name of an object (other than a string),
357 IPython will try to locate the file where it was defined and open the
357 IPython will try to locate the file where it was defined and open the
358 editor at the point where it is defined. You can use `%edit function`
358 editor at the point where it is defined. You can use `%edit function`
359 to load an editor exactly at the point where 'function' is defined,
359 to load an editor exactly at the point where 'function' is defined,
360 edit it and have the file be executed automatically.
360 edit it and have the file be executed automatically.
361
361
362 - If the object is a macro (see %macro for details), this opens up your
362 - If the object is a macro (see %macro for details), this opens up your
363 specified editor with a temporary file containing the macro's data.
363 specified editor with a temporary file containing the macro's data.
364 Upon exit, the macro is reloaded with the contents of the file.
364 Upon exit, the macro is reloaded with the contents of the file.
365
365
366 Note: opening at an exact line is only supported under Unix, and some
366 Note: opening at an exact line is only supported under Unix, and some
367 editors (like kedit and gedit up to Gnome 2.8) do not understand the
367 editors (like kedit and gedit up to Gnome 2.8) do not understand the
368 '+NUMBER' parameter necessary for this feature. Good editors like
368 '+NUMBER' parameter necessary for this feature. Good editors like
369 (X)Emacs, vi, jed, pico and joe all do.
369 (X)Emacs, vi, jed, pico and joe all do.
370
370
371 After executing your code, %edit will return as output the code you
371 After executing your code, %edit will return as output the code you
372 typed in the editor (except when it was an existing file). This way
372 typed in the editor (except when it was an existing file). This way
373 you can reload the code in further invocations of %edit as a variable,
373 you can reload the code in further invocations of %edit as a variable,
374 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
374 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
375 the output.
375 the output.
376
376
377 Note that %edit is also available through the alias %ed.
377 Note that %edit is also available through the alias %ed.
378
378
379 This is an example of creating a simple function inside the editor and
379 This is an example of creating a simple function inside the editor and
380 then modifying it. First, start up the editor::
380 then modifying it. First, start up the editor::
381
381
382 In [1]: ed
382 In [1]: ed
383 Editing... done. Executing edited code...
383 Editing... done. Executing edited code...
384 Out[1]: 'def foo():\\n print "foo() was defined in an editing
384 Out[1]: 'def foo():\\n print "foo() was defined in an editing
385 session"\\n'
385 session"\\n'
386
386
387 We can then call the function foo()::
387 We can then call the function foo()::
388
388
389 In [2]: foo()
389 In [2]: foo()
390 foo() was defined in an editing session
390 foo() was defined in an editing session
391
391
392 Now we edit foo. IPython automatically loads the editor with the
392 Now we edit foo. IPython automatically loads the editor with the
393 (temporary) file where foo() was previously defined::
393 (temporary) file where foo() was previously defined::
394
394
395 In [3]: ed foo
395 In [3]: ed foo
396 Editing... done. Executing edited code...
396 Editing... done. Executing edited code...
397
397
398 And if we call foo() again we get the modified version::
398 And if we call foo() again we get the modified version::
399
399
400 In [4]: foo()
400 In [4]: foo()
401 foo() has now been changed!
401 foo() has now been changed!
402
402
403 Here is an example of how to edit a code snippet successive
403 Here is an example of how to edit a code snippet successive
404 times. First we call the editor::
404 times. First we call the editor::
405
405
406 In [5]: ed
406 In [5]: ed
407 Editing... done. Executing edited code...
407 Editing... done. Executing edited code...
408 hello
408 hello
409 Out[5]: "print 'hello'\\n"
409 Out[5]: "print 'hello'\\n"
410
410
411 Now we call it again with the previous output (stored in _)::
411 Now we call it again with the previous output (stored in _)::
412
412
413 In [6]: ed _
413 In [6]: ed _
414 Editing... done. Executing edited code...
414 Editing... done. Executing edited code...
415 hello world
415 hello world
416 Out[6]: "print 'hello world'\\n"
416 Out[6]: "print 'hello world'\\n"
417
417
418 Now we call it with the output #8 (stored in _8, also as Out[8])::
418 Now we call it with the output #8 (stored in _8, also as Out[8])::
419
419
420 In [7]: ed _8
420 In [7]: ed _8
421 Editing... done. Executing edited code...
421 Editing... done. Executing edited code...
422 hello again
422 hello again
423 Out[7]: "print 'hello again'\\n"
423 Out[7]: "print 'hello again'\\n"
424
424
425
425
426 Changing the default editor hook:
426 Changing the default editor hook:
427
427
428 If you wish to write your own editor hook, you can put it in a
428 If you wish to write your own editor hook, you can put it in a
429 configuration file which you load at startup time. The default hook
429 configuration file which you load at startup time. The default hook
430 is defined in the IPython.core.hooks module, and you can use that as a
430 is defined in the IPython.core.hooks module, and you can use that as a
431 starting example for further modifications. That file also has
431 starting example for further modifications. That file also has
432 general instructions on how to set a new hook for use once you've
432 general instructions on how to set a new hook for use once you've
433 defined it."""
433 defined it."""
434 opts,args = self.parse_options(parameter_s,'prxn:')
434 opts,args = self.parse_options(parameter_s,'prxn:')
435
435
436 try:
436 try:
437 filename, lineno, is_temp = self._find_edit_target(args, opts, last_call)
437 filename, lineno, is_temp = self._find_edit_target(args, opts, last_call)
438 except MacroToEdit as e:
438 except MacroToEdit as e:
439 self._edit_macro(args, e.args[0])
439 self._edit_macro(args, e.args[0])
440 return
440 return
441
441
442 # do actual editing here
442 # do actual editing here
443 print 'Editing...',
443 print 'Editing...',
444 sys.stdout.flush()
444 sys.stdout.flush()
445 try:
445 try:
446 # Quote filenames that may have spaces in them
446 # Quote filenames that may have spaces in them
447 if ' ' in filename:
447 if ' ' in filename:
448 filename = "'%s'" % filename
448 filename = "'%s'" % filename
449 self.shell.hooks.editor(filename,lineno)
449 self.shell.hooks.editor(filename,lineno)
450 except TryNext:
450 except TryNext:
451 warn('Could not open editor')
451 warn('Could not open editor')
452 return
452 return
453
453
454 # XXX TODO: should this be generalized for all string vars?
454 # XXX TODO: should this be generalized for all string vars?
455 # For now, this is special-cased to blocks created by cpaste
455 # For now, this is special-cased to blocks created by cpaste
456 if args.strip() == 'pasted_block':
456 if args.strip() == 'pasted_block':
457 self.shell.user_ns['pasted_block'] = file_read(filename)
457 self.shell.user_ns['pasted_block'] = file_read(filename)
458
458
459 if 'x' in opts: # -x prevents actual execution
459 if 'x' in opts: # -x prevents actual execution
460 print
460 print
461 else:
461 else:
462 print 'done. Executing edited code...'
462 print 'done. Executing edited code...'
463 if 'r' in opts: # Untranslated IPython code
463 if 'r' in opts: # Untranslated IPython code
464 self.shell.run_cell(file_read(filename),
464 self.shell.run_cell(file_read(filename),
465 store_history=False)
465 store_history=False)
466 else:
466 else:
467 self.shell.safe_execfile(filename, self.shell.user_ns,
467 self.shell.safe_execfile(filename, self.shell.user_ns,
468 self.shell.user_ns)
468 self.shell.user_ns)
469
469
470 if is_temp:
470 if is_temp:
471 try:
471 try:
472 return open(filename).read()
472 return open(filename).read()
473 except IOError,msg:
473 except IOError,msg:
474 if msg.filename == filename:
474 if msg.filename == filename:
475 warn('File not found. Did you forget to save?')
475 warn('File not found. Did you forget to save?')
476 return
476 return
477 else:
477 else:
478 self.shell.showtraceback()
478 self.shell.showtraceback()
@@ -1,146 +1,146 b''
1 """Implementation of configuration-related magic functions.
1 """Implementation of configuration-related magic functions.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Stdlib
15 # Stdlib
16 import re
16 import re
17
17
18 # Our own packages
18 # Our own packages
19 from IPython.core.error import UsageError
19 from IPython.core.error import UsageError
20 from IPython.core.magic import Magics, register_magics, line_magic
20 from IPython.core.magic import Magics, magics_class, line_magic
21 from IPython.utils.warn import error
21 from IPython.utils.warn import error
22
22
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24 # Magic implementation classes
24 # Magic implementation classes
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
26
27 @register_magics
27 @magics_class
28 class ConfigMagics(Magics):
28 class ConfigMagics(Magics):
29
29
30 def __init__(self, shell):
30 def __init__(self, shell):
31 super(ConfigMagics, self).__init__(shell)
31 super(ConfigMagics, self).__init__(shell)
32 self.configurables = []
32 self.configurables = []
33
33
34 @line_magic
34 @line_magic
35 def config(self, s):
35 def config(self, s):
36 """configure IPython
36 """configure IPython
37
37
38 %config Class[.trait=value]
38 %config Class[.trait=value]
39
39
40 This magic exposes most of the IPython config system. Any
40 This magic exposes most of the IPython config system. Any
41 Configurable class should be able to be configured with the simple
41 Configurable class should be able to be configured with the simple
42 line::
42 line::
43
43
44 %config Class.trait=value
44 %config Class.trait=value
45
45
46 Where `value` will be resolved in the user's namespace, if it is an
46 Where `value` will be resolved in the user's namespace, if it is an
47 expression or variable name.
47 expression or variable name.
48
48
49 Examples
49 Examples
50 --------
50 --------
51
51
52 To see what classes are available for config, pass no arguments::
52 To see what classes are available for config, pass no arguments::
53
53
54 In [1]: %config
54 In [1]: %config
55 Available objects for config:
55 Available objects for config:
56 TerminalInteractiveShell
56 TerminalInteractiveShell
57 HistoryManager
57 HistoryManager
58 PrefilterManager
58 PrefilterManager
59 AliasManager
59 AliasManager
60 IPCompleter
60 IPCompleter
61 PromptManager
61 PromptManager
62 DisplayFormatter
62 DisplayFormatter
63
63
64 To view what is configurable on a given class, just pass the class
64 To view what is configurable on a given class, just pass the class
65 name::
65 name::
66
66
67 In [2]: %config IPCompleter
67 In [2]: %config IPCompleter
68 IPCompleter options
68 IPCompleter options
69 -----------------
69 -----------------
70 IPCompleter.omit__names=<Enum>
70 IPCompleter.omit__names=<Enum>
71 Current: 2
71 Current: 2
72 Choices: (0, 1, 2)
72 Choices: (0, 1, 2)
73 Instruct the completer to omit private method names
73 Instruct the completer to omit private method names
74 Specifically, when completing on ``object.<tab>``.
74 Specifically, when completing on ``object.<tab>``.
75 When 2 [default]: all names that start with '_' will be excluded.
75 When 2 [default]: all names that start with '_' will be excluded.
76 When 1: all 'magic' names (``__foo__``) will be excluded.
76 When 1: all 'magic' names (``__foo__``) will be excluded.
77 When 0: nothing will be excluded.
77 When 0: nothing will be excluded.
78 IPCompleter.merge_completions=<CBool>
78 IPCompleter.merge_completions=<CBool>
79 Current: True
79 Current: True
80 Whether to merge completion results into a single list
80 Whether to merge completion results into a single list
81 If False, only the completion results from the first non-empty
81 If False, only the completion results from the first non-empty
82 completer will be returned.
82 completer will be returned.
83 IPCompleter.limit_to__all__=<CBool>
83 IPCompleter.limit_to__all__=<CBool>
84 Current: False
84 Current: False
85 Instruct the completer to use __all__ for the completion
85 Instruct the completer to use __all__ for the completion
86 Specifically, when completing on ``object.<tab>``.
86 Specifically, when completing on ``object.<tab>``.
87 When True: only those names in obj.__all__ will be included.
87 When True: only those names in obj.__all__ will be included.
88 When False [default]: the __all__ attribute is ignored
88 When False [default]: the __all__ attribute is ignored
89 IPCompleter.greedy=<CBool>
89 IPCompleter.greedy=<CBool>
90 Current: False
90 Current: False
91 Activate greedy completion
91 Activate greedy completion
92 This will enable completion on elements of lists, results of
92 This will enable completion on elements of lists, results of
93 function calls, etc., but can be unsafe because the code is
93 function calls, etc., but can be unsafe because the code is
94 actually evaluated on TAB.
94 actually evaluated on TAB.
95
95
96 but the real use is in setting values::
96 but the real use is in setting values::
97
97
98 In [3]: %config IPCompleter.greedy = True
98 In [3]: %config IPCompleter.greedy = True
99
99
100 and these values are read from the user_ns if they are variables::
100 and these values are read from the user_ns if they are variables::
101
101
102 In [4]: feeling_greedy=False
102 In [4]: feeling_greedy=False
103
103
104 In [5]: %config IPCompleter.greedy = feeling_greedy
104 In [5]: %config IPCompleter.greedy = feeling_greedy
105
105
106 """
106 """
107 from IPython.config.loader import Config
107 from IPython.config.loader import Config
108 # some IPython objects are Configurable, but do not yet have
108 # some IPython objects are Configurable, but do not yet have
109 # any configurable traits. Exclude them from the effects of
109 # any configurable traits. Exclude them from the effects of
110 # this magic, as their presence is just noise:
110 # this magic, as their presence is just noise:
111 configurables = [ c for c in self.shell.configurables
111 configurables = [ c for c in self.shell.configurables
112 if c.__class__.class_traits(config=True) ]
112 if c.__class__.class_traits(config=True) ]
113 classnames = [ c.__class__.__name__ for c in configurables ]
113 classnames = [ c.__class__.__name__ for c in configurables ]
114
114
115 line = s.strip()
115 line = s.strip()
116 if not line:
116 if not line:
117 # print available configurable names
117 # print available configurable names
118 print "Available objects for config:"
118 print "Available objects for config:"
119 for name in classnames:
119 for name in classnames:
120 print " ", name
120 print " ", name
121 return
121 return
122 elif line in classnames:
122 elif line in classnames:
123 # `%config TerminalInteractiveShell` will print trait info for
123 # `%config TerminalInteractiveShell` will print trait info for
124 # TerminalInteractiveShell
124 # TerminalInteractiveShell
125 c = configurables[classnames.index(line)]
125 c = configurables[classnames.index(line)]
126 cls = c.__class__
126 cls = c.__class__
127 help = cls.class_get_help(c)
127 help = cls.class_get_help(c)
128 # strip leading '--' from cl-args:
128 # strip leading '--' from cl-args:
129 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
129 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
130 print help
130 print help
131 return
131 return
132 elif '=' not in line:
132 elif '=' not in line:
133 raise UsageError("Invalid config statement: %r, "
133 raise UsageError("Invalid config statement: %r, "
134 "should be Class.trait = value" % line)
134 "should be Class.trait = value" % line)
135
135
136 # otherwise, assume we are setting configurables.
136 # otherwise, assume we are setting configurables.
137 # leave quotes on args when splitting, because we want
137 # leave quotes on args when splitting, because we want
138 # unquoted args to eval in user_ns
138 # unquoted args to eval in user_ns
139 cfg = Config()
139 cfg = Config()
140 exec "cfg."+line in locals(), self.shell.user_ns
140 exec "cfg."+line in locals(), self.shell.user_ns
141
141
142 for configurable in configurables:
142 for configurable in configurables:
143 try:
143 try:
144 configurable.update_config(cfg)
144 configurable.update_config(cfg)
145 except Exception as e:
145 except Exception as e:
146 error(e)
146 error(e)
@@ -1,45 +1,45 b''
1 """Deprecated Magic functions.
1 """Deprecated Magic functions.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Our own packages
15 # Our own packages
16 from IPython.core.magic import Magics, register_magics, line_magic
16 from IPython.core.magic import Magics, magics_class, line_magic
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Magic implementation classes
19 # Magic implementation classes
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 @register_magics
22 @magics_class
23 class DeprecatedMagics(Magics):
23 class DeprecatedMagics(Magics):
24 """Magics slated for later removal."""
24 """Magics slated for later removal."""
25
25
26 @line_magic
26 @line_magic
27 def install_profiles(self, parameter_s=''):
27 def install_profiles(self, parameter_s=''):
28 """%install_profiles has been deprecated."""
28 """%install_profiles has been deprecated."""
29 print '\n'.join([
29 print '\n'.join([
30 "%install_profiles has been deprecated.",
30 "%install_profiles has been deprecated.",
31 "Use `ipython profile list` to view available profiles.",
31 "Use `ipython profile list` to view available profiles.",
32 "Requesting a profile with `ipython profile create <name>`",
32 "Requesting a profile with `ipython profile create <name>`",
33 "or `ipython --profile=<name>` will start with the bundled",
33 "or `ipython --profile=<name>` will start with the bundled",
34 "profile of that name if it exists."
34 "profile of that name if it exists."
35 ])
35 ])
36
36
37 @line_magic
37 @line_magic
38 def install_default_config(self, parameter_s=''):
38 def install_default_config(self, parameter_s=''):
39 """%install_default_config has been deprecated."""
39 """%install_default_config has been deprecated."""
40 print '\n'.join([
40 print '\n'.join([
41 "%install_default_config has been deprecated.",
41 "%install_default_config has been deprecated.",
42 "Use `ipython profile create <name>` to initialize a profile",
42 "Use `ipython profile create <name>` to initialize a profile",
43 "with the default config files.",
43 "with the default config files.",
44 "Add `--reset` to overwrite already existing config files with defaults."
44 "Add `--reset` to overwrite already existing config files with defaults."
45 ])
45 ])
@@ -1,955 +1,955 b''
1 """Implementation of execution-related magic functions.
1 """Implementation of execution-related magic functions.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Stdlib
15 # Stdlib
16 import __builtin__ as builtin_mod
16 import __builtin__ as builtin_mod
17 import bdb
17 import bdb
18 import os
18 import os
19 import sys
19 import sys
20 import time
20 import time
21 from StringIO import StringIO
21 from StringIO import StringIO
22
22
23 # cProfile was added in Python2.5
23 # cProfile was added in Python2.5
24 try:
24 try:
25 import cProfile as profile
25 import cProfile as profile
26 import pstats
26 import pstats
27 except ImportError:
27 except ImportError:
28 # profile isn't bundled by default in Debian for license reasons
28 # profile isn't bundled by default in Debian for license reasons
29 try:
29 try:
30 import profile, pstats
30 import profile, pstats
31 except ImportError:
31 except ImportError:
32 profile = pstats = None
32 profile = pstats = None
33
33
34 # Our own packages
34 # Our own packages
35 from IPython.core import debugger, oinspect
35 from IPython.core import debugger, oinspect
36 from IPython.core import page
36 from IPython.core import page
37 from IPython.core.error import UsageError
37 from IPython.core.error import UsageError
38 from IPython.core.macro import Macro
38 from IPython.core.macro import Macro
39 from IPython.core.magic import (Magics, register_magics, line_magic,
39 from IPython.core.magic import (Magics, magics_class, line_magic,
40 on_off, needs_local_scope)
40 on_off, needs_local_scope)
41 from IPython.testing.skipdoctest import skip_doctest
41 from IPython.testing.skipdoctest import skip_doctest
42 from IPython.utils import py3compat
42 from IPython.utils import py3compat
43 from IPython.utils.ipstruct import Struct
43 from IPython.utils.ipstruct import Struct
44 from IPython.utils.module_paths import find_mod
44 from IPython.utils.module_paths import find_mod
45 from IPython.utils.path import get_py_filename, unquote_filename
45 from IPython.utils.path import get_py_filename, unquote_filename
46 from IPython.utils.timing import clock, clock2
46 from IPython.utils.timing import clock, clock2
47 from IPython.utils.warn import warn, error
47 from IPython.utils.warn import warn, error
48
48
49 #-----------------------------------------------------------------------------
49 #-----------------------------------------------------------------------------
50 # Magic implementation classes
50 # Magic implementation classes
51 #-----------------------------------------------------------------------------
51 #-----------------------------------------------------------------------------
52
52
53 @register_magics
53 @magics_class
54 class ExecutionMagics(Magics):
54 class ExecutionMagics(Magics):
55 """Magics related to code execution, debugging, profiling, etc.
55 """Magics related to code execution, debugging, profiling, etc.
56
56
57 """
57 """
58
58
59 def __init__(self, shell):
59 def __init__(self, shell):
60 super(ExecutionMagics, self).__init__(shell)
60 super(ExecutionMagics, self).__init__(shell)
61 if profile is None:
61 if profile is None:
62 self.prun = self.profile_missing_notice
62 self.prun = self.profile_missing_notice
63 # Default execution function used to actually run user code.
63 # Default execution function used to actually run user code.
64 self.default_runner = None
64 self.default_runner = None
65
65
66 def profile_missing_notice(self, *args, **kwargs):
66 def profile_missing_notice(self, *args, **kwargs):
67 error("""\
67 error("""\
68 The profile module could not be found. It has been removed from the standard
68 The profile module could not be found. It has been removed from the standard
69 python packages because of its non-free license. To use profiling, install the
69 python packages because of its non-free license. To use profiling, install the
70 python-profiler package from non-free.""")
70 python-profiler package from non-free.""")
71
71
72 @skip_doctest
72 @skip_doctest
73 @line_magic
73 @line_magic
74 def prun(self, parameter_s='',user_mode=1,
74 def prun(self, parameter_s='',user_mode=1,
75 opts=None,arg_lst=None,prog_ns=None):
75 opts=None,arg_lst=None,prog_ns=None):
76
76
77 """Run a statement through the python code profiler.
77 """Run a statement through the python code profiler.
78
78
79 Usage:
79 Usage:
80 %prun [options] statement
80 %prun [options] statement
81
81
82 The given statement (which doesn't require quote marks) is run via the
82 The given statement (which doesn't require quote marks) is run via the
83 python profiler in a manner similar to the profile.run() function.
83 python profiler in a manner similar to the profile.run() function.
84 Namespaces are internally managed to work correctly; profile.run
84 Namespaces are internally managed to work correctly; profile.run
85 cannot be used in IPython because it makes certain assumptions about
85 cannot be used in IPython because it makes certain assumptions about
86 namespaces which do not hold under IPython.
86 namespaces which do not hold under IPython.
87
87
88 Options:
88 Options:
89
89
90 -l <limit>: you can place restrictions on what or how much of the
90 -l <limit>: you can place restrictions on what or how much of the
91 profile gets printed. The limit value can be:
91 profile gets printed. The limit value can be:
92
92
93 * A string: only information for function names containing this string
93 * A string: only information for function names containing this string
94 is printed.
94 is printed.
95
95
96 * An integer: only these many lines are printed.
96 * An integer: only these many lines are printed.
97
97
98 * A float (between 0 and 1): this fraction of the report is printed
98 * A float (between 0 and 1): this fraction of the report is printed
99 (for example, use a limit of 0.4 to see the topmost 40% only).
99 (for example, use a limit of 0.4 to see the topmost 40% only).
100
100
101 You can combine several limits with repeated use of the option. For
101 You can combine several limits with repeated use of the option. For
102 example, '-l __init__ -l 5' will print only the topmost 5 lines of
102 example, '-l __init__ -l 5' will print only the topmost 5 lines of
103 information about class constructors.
103 information about class constructors.
104
104
105 -r: return the pstats.Stats object generated by the profiling. This
105 -r: return the pstats.Stats object generated by the profiling. This
106 object has all the information about the profile in it, and you can
106 object has all the information about the profile in it, and you can
107 later use it for further analysis or in other functions.
107 later use it for further analysis or in other functions.
108
108
109 -s <key>: sort profile by given key. You can provide more than one key
109 -s <key>: sort profile by given key. You can provide more than one key
110 by using the option several times: '-s key1 -s key2 -s key3...'. The
110 by using the option several times: '-s key1 -s key2 -s key3...'. The
111 default sorting key is 'time'.
111 default sorting key is 'time'.
112
112
113 The following is copied verbatim from the profile documentation
113 The following is copied verbatim from the profile documentation
114 referenced below:
114 referenced below:
115
115
116 When more than one key is provided, additional keys are used as
116 When more than one key is provided, additional keys are used as
117 secondary criteria when the there is equality in all keys selected
117 secondary criteria when the there is equality in all keys selected
118 before them.
118 before them.
119
119
120 Abbreviations can be used for any key names, as long as the
120 Abbreviations can be used for any key names, as long as the
121 abbreviation is unambiguous. The following are the keys currently
121 abbreviation is unambiguous. The following are the keys currently
122 defined:
122 defined:
123
123
124 Valid Arg Meaning
124 Valid Arg Meaning
125 "calls" call count
125 "calls" call count
126 "cumulative" cumulative time
126 "cumulative" cumulative time
127 "file" file name
127 "file" file name
128 "module" file name
128 "module" file name
129 "pcalls" primitive call count
129 "pcalls" primitive call count
130 "line" line number
130 "line" line number
131 "name" function name
131 "name" function name
132 "nfl" name/file/line
132 "nfl" name/file/line
133 "stdname" standard name
133 "stdname" standard name
134 "time" internal time
134 "time" internal time
135
135
136 Note that all sorts on statistics are in descending order (placing
136 Note that all sorts on statistics are in descending order (placing
137 most time consuming items first), where as name, file, and line number
137 most time consuming items first), where as name, file, and line number
138 searches are in ascending order (i.e., alphabetical). The subtle
138 searches are in ascending order (i.e., alphabetical). The subtle
139 distinction between "nfl" and "stdname" is that the standard name is a
139 distinction between "nfl" and "stdname" is that the standard name is a
140 sort of the name as printed, which means that the embedded line
140 sort of the name as printed, which means that the embedded line
141 numbers get compared in an odd way. For example, lines 3, 20, and 40
141 numbers get compared in an odd way. For example, lines 3, 20, and 40
142 would (if the file names were the same) appear in the string order
142 would (if the file names were the same) appear in the string order
143 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
143 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
144 line numbers. In fact, sort_stats("nfl") is the same as
144 line numbers. In fact, sort_stats("nfl") is the same as
145 sort_stats("name", "file", "line").
145 sort_stats("name", "file", "line").
146
146
147 -T <filename>: save profile results as shown on screen to a text
147 -T <filename>: save profile results as shown on screen to a text
148 file. The profile is still shown on screen.
148 file. The profile is still shown on screen.
149
149
150 -D <filename>: save (via dump_stats) profile statistics to given
150 -D <filename>: save (via dump_stats) profile statistics to given
151 filename. This data is in a format understood by the pstats module, and
151 filename. This data is in a format understood by the pstats module, and
152 is generated by a call to the dump_stats() method of profile
152 is generated by a call to the dump_stats() method of profile
153 objects. The profile is still shown on screen.
153 objects. The profile is still shown on screen.
154
154
155 -q: suppress output to the pager. Best used with -T and/or -D above.
155 -q: suppress output to the pager. Best used with -T and/or -D above.
156
156
157 If you want to run complete programs under the profiler's control, use
157 If you want to run complete programs under the profiler's control, use
158 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
158 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
159 contains profiler specific options as described here.
159 contains profiler specific options as described here.
160
160
161 You can read the complete documentation for the profile module with::
161 You can read the complete documentation for the profile module with::
162
162
163 In [1]: import profile; profile.help()
163 In [1]: import profile; profile.help()
164 """
164 """
165
165
166 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
166 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
167
167
168 if user_mode: # regular user call
168 if user_mode: # regular user call
169 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q',
169 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q',
170 list_all=1, posix=False)
170 list_all=1, posix=False)
171 namespace = self.shell.user_ns
171 namespace = self.shell.user_ns
172 else: # called to run a program by %run -p
172 else: # called to run a program by %run -p
173 try:
173 try:
174 filename = get_py_filename(arg_lst[0])
174 filename = get_py_filename(arg_lst[0])
175 except IOError as e:
175 except IOError as e:
176 try:
176 try:
177 msg = str(e)
177 msg = str(e)
178 except UnicodeError:
178 except UnicodeError:
179 msg = e.message
179 msg = e.message
180 error(msg)
180 error(msg)
181 return
181 return
182
182
183 arg_str = 'execfile(filename,prog_ns)'
183 arg_str = 'execfile(filename,prog_ns)'
184 namespace = {
184 namespace = {
185 'execfile': self.shell.safe_execfile,
185 'execfile': self.shell.safe_execfile,
186 'prog_ns': prog_ns,
186 'prog_ns': prog_ns,
187 'filename': filename
187 'filename': filename
188 }
188 }
189
189
190 opts.merge(opts_def)
190 opts.merge(opts_def)
191
191
192 prof = profile.Profile()
192 prof = profile.Profile()
193 try:
193 try:
194 prof = prof.runctx(arg_str,namespace,namespace)
194 prof = prof.runctx(arg_str,namespace,namespace)
195 sys_exit = ''
195 sys_exit = ''
196 except SystemExit:
196 except SystemExit:
197 sys_exit = """*** SystemExit exception caught in code being profiled."""
197 sys_exit = """*** SystemExit exception caught in code being profiled."""
198
198
199 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
199 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
200
200
201 lims = opts.l
201 lims = opts.l
202 if lims:
202 if lims:
203 lims = [] # rebuild lims with ints/floats/strings
203 lims = [] # rebuild lims with ints/floats/strings
204 for lim in opts.l:
204 for lim in opts.l:
205 try:
205 try:
206 lims.append(int(lim))
206 lims.append(int(lim))
207 except ValueError:
207 except ValueError:
208 try:
208 try:
209 lims.append(float(lim))
209 lims.append(float(lim))
210 except ValueError:
210 except ValueError:
211 lims.append(lim)
211 lims.append(lim)
212
212
213 # Trap output.
213 # Trap output.
214 stdout_trap = StringIO()
214 stdout_trap = StringIO()
215
215
216 if hasattr(stats,'stream'):
216 if hasattr(stats,'stream'):
217 # In newer versions of python, the stats object has a 'stream'
217 # In newer versions of python, the stats object has a 'stream'
218 # attribute to write into.
218 # attribute to write into.
219 stats.stream = stdout_trap
219 stats.stream = stdout_trap
220 stats.print_stats(*lims)
220 stats.print_stats(*lims)
221 else:
221 else:
222 # For older versions, we manually redirect stdout during printing
222 # For older versions, we manually redirect stdout during printing
223 sys_stdout = sys.stdout
223 sys_stdout = sys.stdout
224 try:
224 try:
225 sys.stdout = stdout_trap
225 sys.stdout = stdout_trap
226 stats.print_stats(*lims)
226 stats.print_stats(*lims)
227 finally:
227 finally:
228 sys.stdout = sys_stdout
228 sys.stdout = sys_stdout
229
229
230 output = stdout_trap.getvalue()
230 output = stdout_trap.getvalue()
231 output = output.rstrip()
231 output = output.rstrip()
232
232
233 if 'q' not in opts:
233 if 'q' not in opts:
234 page.page(output)
234 page.page(output)
235 print sys_exit,
235 print sys_exit,
236
236
237 dump_file = opts.D[0]
237 dump_file = opts.D[0]
238 text_file = opts.T[0]
238 text_file = opts.T[0]
239 if dump_file:
239 if dump_file:
240 dump_file = unquote_filename(dump_file)
240 dump_file = unquote_filename(dump_file)
241 prof.dump_stats(dump_file)
241 prof.dump_stats(dump_file)
242 print '\n*** Profile stats marshalled to file',\
242 print '\n*** Profile stats marshalled to file',\
243 `dump_file`+'.',sys_exit
243 `dump_file`+'.',sys_exit
244 if text_file:
244 if text_file:
245 text_file = unquote_filename(text_file)
245 text_file = unquote_filename(text_file)
246 pfile = open(text_file,'w')
246 pfile = open(text_file,'w')
247 pfile.write(output)
247 pfile.write(output)
248 pfile.close()
248 pfile.close()
249 print '\n*** Profile printout saved to text file',\
249 print '\n*** Profile printout saved to text file',\
250 `text_file`+'.',sys_exit
250 `text_file`+'.',sys_exit
251
251
252 if opts.has_key('r'):
252 if opts.has_key('r'):
253 return stats
253 return stats
254 else:
254 else:
255 return None
255 return None
256
256
257 @line_magic
257 @line_magic
258 def pdb(self, parameter_s=''):
258 def pdb(self, parameter_s=''):
259 """Control the automatic calling of the pdb interactive debugger.
259 """Control the automatic calling of the pdb interactive debugger.
260
260
261 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
261 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
262 argument it works as a toggle.
262 argument it works as a toggle.
263
263
264 When an exception is triggered, IPython can optionally call the
264 When an exception is triggered, IPython can optionally call the
265 interactive pdb debugger after the traceback printout. %pdb toggles
265 interactive pdb debugger after the traceback printout. %pdb toggles
266 this feature on and off.
266 this feature on and off.
267
267
268 The initial state of this feature is set in your configuration
268 The initial state of this feature is set in your configuration
269 file (the option is ``InteractiveShell.pdb``).
269 file (the option is ``InteractiveShell.pdb``).
270
270
271 If you want to just activate the debugger AFTER an exception has fired,
271 If you want to just activate the debugger AFTER an exception has fired,
272 without having to type '%pdb on' and rerunning your code, you can use
272 without having to type '%pdb on' and rerunning your code, you can use
273 the %debug magic."""
273 the %debug magic."""
274
274
275 par = parameter_s.strip().lower()
275 par = parameter_s.strip().lower()
276
276
277 if par:
277 if par:
278 try:
278 try:
279 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
279 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
280 except KeyError:
280 except KeyError:
281 print ('Incorrect argument. Use on/1, off/0, '
281 print ('Incorrect argument. Use on/1, off/0, '
282 'or nothing for a toggle.')
282 'or nothing for a toggle.')
283 return
283 return
284 else:
284 else:
285 # toggle
285 # toggle
286 new_pdb = not self.shell.call_pdb
286 new_pdb = not self.shell.call_pdb
287
287
288 # set on the shell
288 # set on the shell
289 self.shell.call_pdb = new_pdb
289 self.shell.call_pdb = new_pdb
290 print 'Automatic pdb calling has been turned',on_off(new_pdb)
290 print 'Automatic pdb calling has been turned',on_off(new_pdb)
291
291
292 @line_magic
292 @line_magic
293 def debug(self, parameter_s=''):
293 def debug(self, parameter_s=''):
294 """Activate the interactive debugger in post-mortem mode.
294 """Activate the interactive debugger in post-mortem mode.
295
295
296 If an exception has just occurred, this lets you inspect its stack
296 If an exception has just occurred, this lets you inspect its stack
297 frames interactively. Note that this will always work only on the last
297 frames interactively. Note that this will always work only on the last
298 traceback that occurred, so you must call this quickly after an
298 traceback that occurred, so you must call this quickly after an
299 exception that you wish to inspect has fired, because if another one
299 exception that you wish to inspect has fired, because if another one
300 occurs, it clobbers the previous one.
300 occurs, it clobbers the previous one.
301
301
302 If you want IPython to automatically do this on every exception, see
302 If you want IPython to automatically do this on every exception, see
303 the %pdb magic for more details.
303 the %pdb magic for more details.
304 """
304 """
305 self.shell.debugger(force=True)
305 self.shell.debugger(force=True)
306
306
307 @line_magic
307 @line_magic
308 def tb(self, s):
308 def tb(self, s):
309 """Print the last traceback with the currently active exception mode.
309 """Print the last traceback with the currently active exception mode.
310
310
311 See %xmode for changing exception reporting modes."""
311 See %xmode for changing exception reporting modes."""
312 self.shell.showtraceback()
312 self.shell.showtraceback()
313
313
314 @skip_doctest
314 @skip_doctest
315 @line_magic
315 @line_magic
316 def run(self, parameter_s='', runner=None,
316 def run(self, parameter_s='', runner=None,
317 file_finder=get_py_filename):
317 file_finder=get_py_filename):
318 """Run the named file inside IPython as a program.
318 """Run the named file inside IPython as a program.
319
319
320 Usage:\\
320 Usage:\\
321 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
321 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
322
322
323 Parameters after the filename are passed as command-line arguments to
323 Parameters after the filename are passed as command-line arguments to
324 the program (put in sys.argv). Then, control returns to IPython's
324 the program (put in sys.argv). Then, control returns to IPython's
325 prompt.
325 prompt.
326
326
327 This is similar to running at a system prompt:\\
327 This is similar to running at a system prompt:\\
328 $ python file args\\
328 $ python file args\\
329 but with the advantage of giving you IPython's tracebacks, and of
329 but with the advantage of giving you IPython's tracebacks, and of
330 loading all variables into your interactive namespace for further use
330 loading all variables into your interactive namespace for further use
331 (unless -p is used, see below).
331 (unless -p is used, see below).
332
332
333 The file is executed in a namespace initially consisting only of
333 The file is executed in a namespace initially consisting only of
334 __name__=='__main__' and sys.argv constructed as indicated. It thus
334 __name__=='__main__' and sys.argv constructed as indicated. It thus
335 sees its environment as if it were being run as a stand-alone program
335 sees its environment as if it were being run as a stand-alone program
336 (except for sharing global objects such as previously imported
336 (except for sharing global objects such as previously imported
337 modules). But after execution, the IPython interactive namespace gets
337 modules). But after execution, the IPython interactive namespace gets
338 updated with all variables defined in the program (except for __name__
338 updated with all variables defined in the program (except for __name__
339 and sys.argv). This allows for very convenient loading of code for
339 and sys.argv). This allows for very convenient loading of code for
340 interactive work, while giving each program a 'clean sheet' to run in.
340 interactive work, while giving each program a 'clean sheet' to run in.
341
341
342 Options:
342 Options:
343
343
344 -n: __name__ is NOT set to '__main__', but to the running file's name
344 -n: __name__ is NOT set to '__main__', but to the running file's name
345 without extension (as python does under import). This allows running
345 without extension (as python does under import). This allows running
346 scripts and reloading the definitions in them without calling code
346 scripts and reloading the definitions in them without calling code
347 protected by an ' if __name__ == "__main__" ' clause.
347 protected by an ' if __name__ == "__main__" ' clause.
348
348
349 -i: run the file in IPython's namespace instead of an empty one. This
349 -i: run the file in IPython's namespace instead of an empty one. This
350 is useful if you are experimenting with code written in a text editor
350 is useful if you are experimenting with code written in a text editor
351 which depends on variables defined interactively.
351 which depends on variables defined interactively.
352
352
353 -e: ignore sys.exit() calls or SystemExit exceptions in the script
353 -e: ignore sys.exit() calls or SystemExit exceptions in the script
354 being run. This is particularly useful if IPython is being used to
354 being run. This is particularly useful if IPython is being used to
355 run unittests, which always exit with a sys.exit() call. In such
355 run unittests, which always exit with a sys.exit() call. In such
356 cases you are interested in the output of the test results, not in
356 cases you are interested in the output of the test results, not in
357 seeing a traceback of the unittest module.
357 seeing a traceback of the unittest module.
358
358
359 -t: print timing information at the end of the run. IPython will give
359 -t: print timing information at the end of the run. IPython will give
360 you an estimated CPU time consumption for your script, which under
360 you an estimated CPU time consumption for your script, which under
361 Unix uses the resource module to avoid the wraparound problems of
361 Unix uses the resource module to avoid the wraparound problems of
362 time.clock(). Under Unix, an estimate of time spent on system tasks
362 time.clock(). Under Unix, an estimate of time spent on system tasks
363 is also given (for Windows platforms this is reported as 0.0).
363 is also given (for Windows platforms this is reported as 0.0).
364
364
365 If -t is given, an additional -N<N> option can be given, where <N>
365 If -t is given, an additional -N<N> option can be given, where <N>
366 must be an integer indicating how many times you want the script to
366 must be an integer indicating how many times you want the script to
367 run. The final timing report will include total and per run results.
367 run. The final timing report will include total and per run results.
368
368
369 For example (testing the script uniq_stable.py)::
369 For example (testing the script uniq_stable.py)::
370
370
371 In [1]: run -t uniq_stable
371 In [1]: run -t uniq_stable
372
372
373 IPython CPU timings (estimated):\\
373 IPython CPU timings (estimated):\\
374 User : 0.19597 s.\\
374 User : 0.19597 s.\\
375 System: 0.0 s.\\
375 System: 0.0 s.\\
376
376
377 In [2]: run -t -N5 uniq_stable
377 In [2]: run -t -N5 uniq_stable
378
378
379 IPython CPU timings (estimated):\\
379 IPython CPU timings (estimated):\\
380 Total runs performed: 5\\
380 Total runs performed: 5\\
381 Times : Total Per run\\
381 Times : Total Per run\\
382 User : 0.910862 s, 0.1821724 s.\\
382 User : 0.910862 s, 0.1821724 s.\\
383 System: 0.0 s, 0.0 s.
383 System: 0.0 s, 0.0 s.
384
384
385 -d: run your program under the control of pdb, the Python debugger.
385 -d: run your program under the control of pdb, the Python debugger.
386 This allows you to execute your program step by step, watch variables,
386 This allows you to execute your program step by step, watch variables,
387 etc. Internally, what IPython does is similar to calling:
387 etc. Internally, what IPython does is similar to calling:
388
388
389 pdb.run('execfile("YOURFILENAME")')
389 pdb.run('execfile("YOURFILENAME")')
390
390
391 with a breakpoint set on line 1 of your file. You can change the line
391 with a breakpoint set on line 1 of your file. You can change the line
392 number for this automatic breakpoint to be <N> by using the -bN option
392 number for this automatic breakpoint to be <N> by using the -bN option
393 (where N must be an integer). For example::
393 (where N must be an integer). For example::
394
394
395 %run -d -b40 myscript
395 %run -d -b40 myscript
396
396
397 will set the first breakpoint at line 40 in myscript.py. Note that
397 will set the first breakpoint at line 40 in myscript.py. Note that
398 the first breakpoint must be set on a line which actually does
398 the first breakpoint must be set on a line which actually does
399 something (not a comment or docstring) for it to stop execution.
399 something (not a comment or docstring) for it to stop execution.
400
400
401 When the pdb debugger starts, you will see a (Pdb) prompt. You must
401 When the pdb debugger starts, you will see a (Pdb) prompt. You must
402 first enter 'c' (without quotes) to start execution up to the first
402 first enter 'c' (without quotes) to start execution up to the first
403 breakpoint.
403 breakpoint.
404
404
405 Entering 'help' gives information about the use of the debugger. You
405 Entering 'help' gives information about the use of the debugger. You
406 can easily see pdb's full documentation with "import pdb;pdb.help()"
406 can easily see pdb's full documentation with "import pdb;pdb.help()"
407 at a prompt.
407 at a prompt.
408
408
409 -p: run program under the control of the Python profiler module (which
409 -p: run program under the control of the Python profiler module (which
410 prints a detailed report of execution times, function calls, etc).
410 prints a detailed report of execution times, function calls, etc).
411
411
412 You can pass other options after -p which affect the behavior of the
412 You can pass other options after -p which affect the behavior of the
413 profiler itself. See the docs for %prun for details.
413 profiler itself. See the docs for %prun for details.
414
414
415 In this mode, the program's variables do NOT propagate back to the
415 In this mode, the program's variables do NOT propagate back to the
416 IPython interactive namespace (because they remain in the namespace
416 IPython interactive namespace (because they remain in the namespace
417 where the profiler executes them).
417 where the profiler executes them).
418
418
419 Internally this triggers a call to %prun, see its documentation for
419 Internally this triggers a call to %prun, see its documentation for
420 details on the options available specifically for profiling.
420 details on the options available specifically for profiling.
421
421
422 There is one special usage for which the text above doesn't apply:
422 There is one special usage for which the text above doesn't apply:
423 if the filename ends with .ipy, the file is run as ipython script,
423 if the filename ends with .ipy, the file is run as ipython script,
424 just as if the commands were written on IPython prompt.
424 just as if the commands were written on IPython prompt.
425
425
426 -m: specify module name to load instead of script path. Similar to
426 -m: specify module name to load instead of script path. Similar to
427 the -m option for the python interpreter. Use this option last if you
427 the -m option for the python interpreter. Use this option last if you
428 want to combine with other %run options. Unlike the python interpreter
428 want to combine with other %run options. Unlike the python interpreter
429 only source modules are allowed no .pyc or .pyo files.
429 only source modules are allowed no .pyc or .pyo files.
430 For example::
430 For example::
431
431
432 %run -m example
432 %run -m example
433
433
434 will run the example module.
434 will run the example module.
435
435
436 """
436 """
437
437
438 # get arguments and set sys.argv for program to be run.
438 # get arguments and set sys.argv for program to be run.
439 opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:',
439 opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:',
440 mode='list', list_all=1)
440 mode='list', list_all=1)
441 if "m" in opts:
441 if "m" in opts:
442 modulename = opts["m"][0]
442 modulename = opts["m"][0]
443 modpath = find_mod(modulename)
443 modpath = find_mod(modulename)
444 if modpath is None:
444 if modpath is None:
445 warn('%r is not a valid modulename on sys.path'%modulename)
445 warn('%r is not a valid modulename on sys.path'%modulename)
446 return
446 return
447 arg_lst = [modpath] + arg_lst
447 arg_lst = [modpath] + arg_lst
448 try:
448 try:
449 filename = file_finder(arg_lst[0])
449 filename = file_finder(arg_lst[0])
450 except IndexError:
450 except IndexError:
451 warn('you must provide at least a filename.')
451 warn('you must provide at least a filename.')
452 print '\n%run:\n', oinspect.getdoc(self.run)
452 print '\n%run:\n', oinspect.getdoc(self.run)
453 return
453 return
454 except IOError as e:
454 except IOError as e:
455 try:
455 try:
456 msg = str(e)
456 msg = str(e)
457 except UnicodeError:
457 except UnicodeError:
458 msg = e.message
458 msg = e.message
459 error(msg)
459 error(msg)
460 return
460 return
461
461
462 if filename.lower().endswith('.ipy'):
462 if filename.lower().endswith('.ipy'):
463 self.shell.safe_execfile_ipy(filename)
463 self.shell.safe_execfile_ipy(filename)
464 return
464 return
465
465
466 # Control the response to exit() calls made by the script being run
466 # Control the response to exit() calls made by the script being run
467 exit_ignore = 'e' in opts
467 exit_ignore = 'e' in opts
468
468
469 # Make sure that the running script gets a proper sys.argv as if it
469 # Make sure that the running script gets a proper sys.argv as if it
470 # were run from a system shell.
470 # were run from a system shell.
471 save_argv = sys.argv # save it for later restoring
471 save_argv = sys.argv # save it for later restoring
472
472
473 # simulate shell expansion on arguments, at least tilde expansion
473 # simulate shell expansion on arguments, at least tilde expansion
474 args = [ os.path.expanduser(a) for a in arg_lst[1:] ]
474 args = [ os.path.expanduser(a) for a in arg_lst[1:] ]
475
475
476 sys.argv = [filename] + args # put in the proper filename
476 sys.argv = [filename] + args # put in the proper filename
477 # protect sys.argv from potential unicode strings on Python 2:
477 # protect sys.argv from potential unicode strings on Python 2:
478 if not py3compat.PY3:
478 if not py3compat.PY3:
479 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
479 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
480
480
481 if 'i' in opts:
481 if 'i' in opts:
482 # Run in user's interactive namespace
482 # Run in user's interactive namespace
483 prog_ns = self.shell.user_ns
483 prog_ns = self.shell.user_ns
484 __name__save = self.shell.user_ns['__name__']
484 __name__save = self.shell.user_ns['__name__']
485 prog_ns['__name__'] = '__main__'
485 prog_ns['__name__'] = '__main__'
486 main_mod = self.shell.new_main_mod(prog_ns)
486 main_mod = self.shell.new_main_mod(prog_ns)
487 else:
487 else:
488 # Run in a fresh, empty namespace
488 # Run in a fresh, empty namespace
489 if 'n' in opts:
489 if 'n' in opts:
490 name = os.path.splitext(os.path.basename(filename))[0]
490 name = os.path.splitext(os.path.basename(filename))[0]
491 else:
491 else:
492 name = '__main__'
492 name = '__main__'
493
493
494 main_mod = self.shell.new_main_mod()
494 main_mod = self.shell.new_main_mod()
495 prog_ns = main_mod.__dict__
495 prog_ns = main_mod.__dict__
496 prog_ns['__name__'] = name
496 prog_ns['__name__'] = name
497
497
498 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
498 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
499 # set the __file__ global in the script's namespace
499 # set the __file__ global in the script's namespace
500 prog_ns['__file__'] = filename
500 prog_ns['__file__'] = filename
501
501
502 # pickle fix. See interactiveshell for an explanation. But we need to
502 # pickle fix. See interactiveshell for an explanation. But we need to
503 # make sure that, if we overwrite __main__, we replace it at the end
503 # make sure that, if we overwrite __main__, we replace it at the end
504 main_mod_name = prog_ns['__name__']
504 main_mod_name = prog_ns['__name__']
505
505
506 if main_mod_name == '__main__':
506 if main_mod_name == '__main__':
507 restore_main = sys.modules['__main__']
507 restore_main = sys.modules['__main__']
508 else:
508 else:
509 restore_main = False
509 restore_main = False
510
510
511 # This needs to be undone at the end to prevent holding references to
511 # This needs to be undone at the end to prevent holding references to
512 # every single object ever created.
512 # every single object ever created.
513 sys.modules[main_mod_name] = main_mod
513 sys.modules[main_mod_name] = main_mod
514
514
515 try:
515 try:
516 stats = None
516 stats = None
517 with self.shell.readline_no_record:
517 with self.shell.readline_no_record:
518 if 'p' in opts:
518 if 'p' in opts:
519 stats = self.prun('', 0, opts, arg_lst, prog_ns)
519 stats = self.prun('', 0, opts, arg_lst, prog_ns)
520 else:
520 else:
521 if 'd' in opts:
521 if 'd' in opts:
522 deb = debugger.Pdb(self.shell.colors)
522 deb = debugger.Pdb(self.shell.colors)
523 # reset Breakpoint state, which is moronically kept
523 # reset Breakpoint state, which is moronically kept
524 # in a class
524 # in a class
525 bdb.Breakpoint.next = 1
525 bdb.Breakpoint.next = 1
526 bdb.Breakpoint.bplist = {}
526 bdb.Breakpoint.bplist = {}
527 bdb.Breakpoint.bpbynumber = [None]
527 bdb.Breakpoint.bpbynumber = [None]
528 # Set an initial breakpoint to stop execution
528 # Set an initial breakpoint to stop execution
529 maxtries = 10
529 maxtries = 10
530 bp = int(opts.get('b', [1])[0])
530 bp = int(opts.get('b', [1])[0])
531 checkline = deb.checkline(filename, bp)
531 checkline = deb.checkline(filename, bp)
532 if not checkline:
532 if not checkline:
533 for bp in range(bp + 1, bp + maxtries + 1):
533 for bp in range(bp + 1, bp + maxtries + 1):
534 if deb.checkline(filename, bp):
534 if deb.checkline(filename, bp):
535 break
535 break
536 else:
536 else:
537 msg = ("\nI failed to find a valid line to set "
537 msg = ("\nI failed to find a valid line to set "
538 "a breakpoint\n"
538 "a breakpoint\n"
539 "after trying up to line: %s.\n"
539 "after trying up to line: %s.\n"
540 "Please set a valid breakpoint manually "
540 "Please set a valid breakpoint manually "
541 "with the -b option." % bp)
541 "with the -b option." % bp)
542 error(msg)
542 error(msg)
543 return
543 return
544 # if we find a good linenumber, set the breakpoint
544 # if we find a good linenumber, set the breakpoint
545 deb.do_break('%s:%s' % (filename, bp))
545 deb.do_break('%s:%s' % (filename, bp))
546 # Start file run
546 # Start file run
547 print "NOTE: Enter 'c' at the",
547 print "NOTE: Enter 'c' at the",
548 print "%s prompt to start your script." % deb.prompt
548 print "%s prompt to start your script." % deb.prompt
549 ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns}
549 ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns}
550 try:
550 try:
551 deb.run('execfile("%s", prog_ns)' % filename, ns)
551 deb.run('execfile("%s", prog_ns)' % filename, ns)
552
552
553 except:
553 except:
554 etype, value, tb = sys.exc_info()
554 etype, value, tb = sys.exc_info()
555 # Skip three frames in the traceback: the %run one,
555 # Skip three frames in the traceback: the %run one,
556 # one inside bdb.py, and the command-line typed by the
556 # one inside bdb.py, and the command-line typed by the
557 # user (run by exec in pdb itself).
557 # user (run by exec in pdb itself).
558 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
558 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
559 else:
559 else:
560 if runner is None:
560 if runner is None:
561 runner = self.default_runner
561 runner = self.default_runner
562 if runner is None:
562 if runner is None:
563 runner = self.shell.safe_execfile
563 runner = self.shell.safe_execfile
564 if 't' in opts:
564 if 't' in opts:
565 # timed execution
565 # timed execution
566 try:
566 try:
567 nruns = int(opts['N'][0])
567 nruns = int(opts['N'][0])
568 if nruns < 1:
568 if nruns < 1:
569 error('Number of runs must be >=1')
569 error('Number of runs must be >=1')
570 return
570 return
571 except (KeyError):
571 except (KeyError):
572 nruns = 1
572 nruns = 1
573 twall0 = time.time()
573 twall0 = time.time()
574 if nruns == 1:
574 if nruns == 1:
575 t0 = clock2()
575 t0 = clock2()
576 runner(filename, prog_ns, prog_ns,
576 runner(filename, prog_ns, prog_ns,
577 exit_ignore=exit_ignore)
577 exit_ignore=exit_ignore)
578 t1 = clock2()
578 t1 = clock2()
579 t_usr = t1[0] - t0[0]
579 t_usr = t1[0] - t0[0]
580 t_sys = t1[1] - t0[1]
580 t_sys = t1[1] - t0[1]
581 print "\nIPython CPU timings (estimated):"
581 print "\nIPython CPU timings (estimated):"
582 print " User : %10.2f s." % t_usr
582 print " User : %10.2f s." % t_usr
583 print " System : %10.2f s." % t_sys
583 print " System : %10.2f s." % t_sys
584 else:
584 else:
585 runs = range(nruns)
585 runs = range(nruns)
586 t0 = clock2()
586 t0 = clock2()
587 for nr in runs:
587 for nr in runs:
588 runner(filename, prog_ns, prog_ns,
588 runner(filename, prog_ns, prog_ns,
589 exit_ignore=exit_ignore)
589 exit_ignore=exit_ignore)
590 t1 = clock2()
590 t1 = clock2()
591 t_usr = t1[0] - t0[0]
591 t_usr = t1[0] - t0[0]
592 t_sys = t1[1] - t0[1]
592 t_sys = t1[1] - t0[1]
593 print "\nIPython CPU timings (estimated):"
593 print "\nIPython CPU timings (estimated):"
594 print "Total runs performed:", nruns
594 print "Total runs performed:", nruns
595 print " Times : %10.2f %10.2f" % ('Total', 'Per run')
595 print " Times : %10.2f %10.2f" % ('Total', 'Per run')
596 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
596 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
597 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
597 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
598 twall1 = time.time()
598 twall1 = time.time()
599 print "Wall time: %10.2f s." % (twall1 - twall0)
599 print "Wall time: %10.2f s." % (twall1 - twall0)
600
600
601 else:
601 else:
602 # regular execution
602 # regular execution
603 runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore)
603 runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore)
604
604
605 if 'i' in opts:
605 if 'i' in opts:
606 self.shell.user_ns['__name__'] = __name__save
606 self.shell.user_ns['__name__'] = __name__save
607 else:
607 else:
608 # The shell MUST hold a reference to prog_ns so after %run
608 # The shell MUST hold a reference to prog_ns so after %run
609 # exits, the python deletion mechanism doesn't zero it out
609 # exits, the python deletion mechanism doesn't zero it out
610 # (leaving dangling references).
610 # (leaving dangling references).
611 self.shell.cache_main_mod(prog_ns, filename)
611 self.shell.cache_main_mod(prog_ns, filename)
612 # update IPython interactive namespace
612 # update IPython interactive namespace
613
613
614 # Some forms of read errors on the file may mean the
614 # Some forms of read errors on the file may mean the
615 # __name__ key was never set; using pop we don't have to
615 # __name__ key was never set; using pop we don't have to
616 # worry about a possible KeyError.
616 # worry about a possible KeyError.
617 prog_ns.pop('__name__', None)
617 prog_ns.pop('__name__', None)
618
618
619 self.shell.user_ns.update(prog_ns)
619 self.shell.user_ns.update(prog_ns)
620 finally:
620 finally:
621 # It's a bit of a mystery why, but __builtins__ can change from
621 # It's a bit of a mystery why, but __builtins__ can change from
622 # being a module to becoming a dict missing some key data after
622 # being a module to becoming a dict missing some key data after
623 # %run. As best I can see, this is NOT something IPython is doing
623 # %run. As best I can see, this is NOT something IPython is doing
624 # at all, and similar problems have been reported before:
624 # at all, and similar problems have been reported before:
625 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
625 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
626 # Since this seems to be done by the interpreter itself, the best
626 # Since this seems to be done by the interpreter itself, the best
627 # we can do is to at least restore __builtins__ for the user on
627 # we can do is to at least restore __builtins__ for the user on
628 # exit.
628 # exit.
629 self.shell.user_ns['__builtins__'] = builtin_mod
629 self.shell.user_ns['__builtins__'] = builtin_mod
630
630
631 # Ensure key global structures are restored
631 # Ensure key global structures are restored
632 sys.argv = save_argv
632 sys.argv = save_argv
633 if restore_main:
633 if restore_main:
634 sys.modules['__main__'] = restore_main
634 sys.modules['__main__'] = restore_main
635 else:
635 else:
636 # Remove from sys.modules the reference to main_mod we'd
636 # Remove from sys.modules the reference to main_mod we'd
637 # added. Otherwise it will trap references to objects
637 # added. Otherwise it will trap references to objects
638 # contained therein.
638 # contained therein.
639 del sys.modules[main_mod_name]
639 del sys.modules[main_mod_name]
640
640
641 return stats
641 return stats
642
642
643 @skip_doctest
643 @skip_doctest
644 @line_magic
644 @line_magic
645 def timeit(self, parameter_s=''):
645 def timeit(self, parameter_s=''):
646 """Time execution of a Python statement or expression
646 """Time execution of a Python statement or expression
647
647
648 Usage:\\
648 Usage:\\
649 %timeit [-n<N> -r<R> [-t|-c]] statement
649 %timeit [-n<N> -r<R> [-t|-c]] statement
650
650
651 Time execution of a Python statement or expression using the timeit
651 Time execution of a Python statement or expression using the timeit
652 module.
652 module.
653
653
654 Options:
654 Options:
655 -n<N>: execute the given statement <N> times in a loop. If this value
655 -n<N>: execute the given statement <N> times in a loop. If this value
656 is not given, a fitting value is chosen.
656 is not given, a fitting value is chosen.
657
657
658 -r<R>: repeat the loop iteration <R> times and take the best result.
658 -r<R>: repeat the loop iteration <R> times and take the best result.
659 Default: 3
659 Default: 3
660
660
661 -t: use time.time to measure the time, which is the default on Unix.
661 -t: use time.time to measure the time, which is the default on Unix.
662 This function measures wall time.
662 This function measures wall time.
663
663
664 -c: use time.clock to measure the time, which is the default on
664 -c: use time.clock to measure the time, which is the default on
665 Windows and measures wall time. On Unix, resource.getrusage is used
665 Windows and measures wall time. On Unix, resource.getrusage is used
666 instead and returns the CPU user time.
666 instead and returns the CPU user time.
667
667
668 -p<P>: use a precision of <P> digits to display the timing result.
668 -p<P>: use a precision of <P> digits to display the timing result.
669 Default: 3
669 Default: 3
670
670
671
671
672 Examples
672 Examples
673 --------
673 --------
674 ::
674 ::
675
675
676 In [1]: %timeit pass
676 In [1]: %timeit pass
677 10000000 loops, best of 3: 53.3 ns per loop
677 10000000 loops, best of 3: 53.3 ns per loop
678
678
679 In [2]: u = None
679 In [2]: u = None
680
680
681 In [3]: %timeit u is None
681 In [3]: %timeit u is None
682 10000000 loops, best of 3: 184 ns per loop
682 10000000 loops, best of 3: 184 ns per loop
683
683
684 In [4]: %timeit -r 4 u == None
684 In [4]: %timeit -r 4 u == None
685 1000000 loops, best of 4: 242 ns per loop
685 1000000 loops, best of 4: 242 ns per loop
686
686
687 In [5]: import time
687 In [5]: import time
688
688
689 In [6]: %timeit -n1 time.sleep(2)
689 In [6]: %timeit -n1 time.sleep(2)
690 1 loops, best of 3: 2 s per loop
690 1 loops, best of 3: 2 s per loop
691
691
692
692
693 The times reported by %timeit will be slightly higher than those
693 The times reported by %timeit will be slightly higher than those
694 reported by the timeit.py script when variables are accessed. This is
694 reported by the timeit.py script when variables are accessed. This is
695 due to the fact that %timeit executes the statement in the namespace
695 due to the fact that %timeit executes the statement in the namespace
696 of the shell, compared with timeit.py, which uses a single setup
696 of the shell, compared with timeit.py, which uses a single setup
697 statement to import function or create variables. Generally, the bias
697 statement to import function or create variables. Generally, the bias
698 does not matter as long as results from timeit.py are not mixed with
698 does not matter as long as results from timeit.py are not mixed with
699 those from %timeit."""
699 those from %timeit."""
700
700
701 import timeit
701 import timeit
702 import math
702 import math
703
703
704 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
704 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
705 # certain terminals. Until we figure out a robust way of
705 # certain terminals. Until we figure out a robust way of
706 # auto-detecting if the terminal can deal with it, use plain 'us' for
706 # auto-detecting if the terminal can deal with it, use plain 'us' for
707 # microseconds. I am really NOT happy about disabling the proper
707 # microseconds. I am really NOT happy about disabling the proper
708 # 'micro' prefix, but crashing is worse... If anyone knows what the
708 # 'micro' prefix, but crashing is worse... If anyone knows what the
709 # right solution for this is, I'm all ears...
709 # right solution for this is, I'm all ears...
710 #
710 #
711 # Note: using
711 # Note: using
712 #
712 #
713 # s = u'\xb5'
713 # s = u'\xb5'
714 # s.encode(sys.getdefaultencoding())
714 # s.encode(sys.getdefaultencoding())
715 #
715 #
716 # is not sufficient, as I've seen terminals where that fails but
716 # is not sufficient, as I've seen terminals where that fails but
717 # print s
717 # print s
718 #
718 #
719 # succeeds
719 # succeeds
720 #
720 #
721 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
721 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
722
722
723 #units = [u"s", u"ms",u'\xb5',"ns"]
723 #units = [u"s", u"ms",u'\xb5',"ns"]
724 units = [u"s", u"ms",u'us',"ns"]
724 units = [u"s", u"ms",u'us',"ns"]
725
725
726 scaling = [1, 1e3, 1e6, 1e9]
726 scaling = [1, 1e3, 1e6, 1e9]
727
727
728 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
728 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
729 posix=False, strict=False)
729 posix=False, strict=False)
730 if stmt == "":
730 if stmt == "":
731 return
731 return
732 timefunc = timeit.default_timer
732 timefunc = timeit.default_timer
733 number = int(getattr(opts, "n", 0))
733 number = int(getattr(opts, "n", 0))
734 repeat = int(getattr(opts, "r", timeit.default_repeat))
734 repeat = int(getattr(opts, "r", timeit.default_repeat))
735 precision = int(getattr(opts, "p", 3))
735 precision = int(getattr(opts, "p", 3))
736 if hasattr(opts, "t"):
736 if hasattr(opts, "t"):
737 timefunc = time.time
737 timefunc = time.time
738 if hasattr(opts, "c"):
738 if hasattr(opts, "c"):
739 timefunc = clock
739 timefunc = clock
740
740
741 timer = timeit.Timer(timer=timefunc)
741 timer = timeit.Timer(timer=timefunc)
742 # this code has tight coupling to the inner workings of timeit.Timer,
742 # this code has tight coupling to the inner workings of timeit.Timer,
743 # but is there a better way to achieve that the code stmt has access
743 # but is there a better way to achieve that the code stmt has access
744 # to the shell namespace?
744 # to the shell namespace?
745
745
746 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
746 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
747 'setup': "pass"}
747 'setup': "pass"}
748 # Track compilation time so it can be reported if too long
748 # Track compilation time so it can be reported if too long
749 # Minimum time above which compilation time will be reported
749 # Minimum time above which compilation time will be reported
750 tc_min = 0.1
750 tc_min = 0.1
751
751
752 t0 = clock()
752 t0 = clock()
753 code = compile(src, "<magic-timeit>", "exec")
753 code = compile(src, "<magic-timeit>", "exec")
754 tc = clock()-t0
754 tc = clock()-t0
755
755
756 ns = {}
756 ns = {}
757 exec code in self.shell.user_ns, ns
757 exec code in self.shell.user_ns, ns
758 timer.inner = ns["inner"]
758 timer.inner = ns["inner"]
759
759
760 if number == 0:
760 if number == 0:
761 # determine number so that 0.2 <= total time < 2.0
761 # determine number so that 0.2 <= total time < 2.0
762 number = 1
762 number = 1
763 for i in range(1, 10):
763 for i in range(1, 10):
764 if timer.timeit(number) >= 0.2:
764 if timer.timeit(number) >= 0.2:
765 break
765 break
766 number *= 10
766 number *= 10
767
767
768 best = min(timer.repeat(repeat, number)) / number
768 best = min(timer.repeat(repeat, number)) / number
769
769
770 if best > 0.0 and best < 1000.0:
770 if best > 0.0 and best < 1000.0:
771 order = min(-int(math.floor(math.log10(best)) // 3), 3)
771 order = min(-int(math.floor(math.log10(best)) // 3), 3)
772 elif best >= 1000.0:
772 elif best >= 1000.0:
773 order = 0
773 order = 0
774 else:
774 else:
775 order = 3
775 order = 3
776 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
776 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
777 precision,
777 precision,
778 best * scaling[order],
778 best * scaling[order],
779 units[order])
779 units[order])
780 if tc > tc_min:
780 if tc > tc_min:
781 print "Compiler time: %.2f s" % tc
781 print "Compiler time: %.2f s" % tc
782
782
783 @skip_doctest
783 @skip_doctest
784 @needs_local_scope
784 @needs_local_scope
785 @line_magic
785 @line_magic
786 def time(self,parameter_s, user_locals):
786 def time(self,parameter_s, user_locals):
787 """Time execution of a Python statement or expression.
787 """Time execution of a Python statement or expression.
788
788
789 The CPU and wall clock times are printed, and the value of the
789 The CPU and wall clock times are printed, and the value of the
790 expression (if any) is returned. Note that under Win32, system time
790 expression (if any) is returned. Note that under Win32, system time
791 is always reported as 0, since it can not be measured.
791 is always reported as 0, since it can not be measured.
792
792
793 This function provides very basic timing functionality. In Python
793 This function provides very basic timing functionality. In Python
794 2.3, the timeit module offers more control and sophistication, so this
794 2.3, the timeit module offers more control and sophistication, so this
795 could be rewritten to use it (patches welcome).
795 could be rewritten to use it (patches welcome).
796
796
797 Examples
797 Examples
798 --------
798 --------
799 ::
799 ::
800
800
801 In [1]: time 2**128
801 In [1]: time 2**128
802 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
802 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
803 Wall time: 0.00
803 Wall time: 0.00
804 Out[1]: 340282366920938463463374607431768211456L
804 Out[1]: 340282366920938463463374607431768211456L
805
805
806 In [2]: n = 1000000
806 In [2]: n = 1000000
807
807
808 In [3]: time sum(range(n))
808 In [3]: time sum(range(n))
809 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
809 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
810 Wall time: 1.37
810 Wall time: 1.37
811 Out[3]: 499999500000L
811 Out[3]: 499999500000L
812
812
813 In [4]: time print 'hello world'
813 In [4]: time print 'hello world'
814 hello world
814 hello world
815 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
815 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
816 Wall time: 0.00
816 Wall time: 0.00
817
817
818 Note that the time needed by Python to compile the given expression
818 Note that the time needed by Python to compile the given expression
819 will be reported if it is more than 0.1s. In this example, the
819 will be reported if it is more than 0.1s. In this example, the
820 actual exponentiation is done by Python at compilation time, so while
820 actual exponentiation is done by Python at compilation time, so while
821 the expression can take a noticeable amount of time to compute, that
821 the expression can take a noticeable amount of time to compute, that
822 time is purely due to the compilation:
822 time is purely due to the compilation:
823
823
824 In [5]: time 3**9999;
824 In [5]: time 3**9999;
825 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
825 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
826 Wall time: 0.00 s
826 Wall time: 0.00 s
827
827
828 In [6]: time 3**999999;
828 In [6]: time 3**999999;
829 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
829 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
830 Wall time: 0.00 s
830 Wall time: 0.00 s
831 Compiler : 0.78 s
831 Compiler : 0.78 s
832 """
832 """
833
833
834 # fail immediately if the given expression can't be compiled
834 # fail immediately if the given expression can't be compiled
835
835
836 expr = self.shell.prefilter(parameter_s,False)
836 expr = self.shell.prefilter(parameter_s,False)
837
837
838 # Minimum time above which compilation time will be reported
838 # Minimum time above which compilation time will be reported
839 tc_min = 0.1
839 tc_min = 0.1
840
840
841 try:
841 try:
842 mode = 'eval'
842 mode = 'eval'
843 t0 = clock()
843 t0 = clock()
844 code = compile(expr,'<timed eval>',mode)
844 code = compile(expr,'<timed eval>',mode)
845 tc = clock()-t0
845 tc = clock()-t0
846 except SyntaxError:
846 except SyntaxError:
847 mode = 'exec'
847 mode = 'exec'
848 t0 = clock()
848 t0 = clock()
849 code = compile(expr,'<timed exec>',mode)
849 code = compile(expr,'<timed exec>',mode)
850 tc = clock()-t0
850 tc = clock()-t0
851 # skew measurement as little as possible
851 # skew measurement as little as possible
852 glob = self.shell.user_ns
852 glob = self.shell.user_ns
853 wtime = time.time
853 wtime = time.time
854 # time execution
854 # time execution
855 wall_st = wtime()
855 wall_st = wtime()
856 if mode=='eval':
856 if mode=='eval':
857 st = clock2()
857 st = clock2()
858 out = eval(code, glob, user_locals)
858 out = eval(code, glob, user_locals)
859 end = clock2()
859 end = clock2()
860 else:
860 else:
861 st = clock2()
861 st = clock2()
862 exec code in glob, user_locals
862 exec code in glob, user_locals
863 end = clock2()
863 end = clock2()
864 out = None
864 out = None
865 wall_end = wtime()
865 wall_end = wtime()
866 # Compute actual times and report
866 # Compute actual times and report
867 wall_time = wall_end-wall_st
867 wall_time = wall_end-wall_st
868 cpu_user = end[0]-st[0]
868 cpu_user = end[0]-st[0]
869 cpu_sys = end[1]-st[1]
869 cpu_sys = end[1]-st[1]
870 cpu_tot = cpu_user+cpu_sys
870 cpu_tot = cpu_user+cpu_sys
871 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
871 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
872 (cpu_user,cpu_sys,cpu_tot)
872 (cpu_user,cpu_sys,cpu_tot)
873 print "Wall time: %.2f s" % wall_time
873 print "Wall time: %.2f s" % wall_time
874 if tc > tc_min:
874 if tc > tc_min:
875 print "Compiler : %.2f s" % tc
875 print "Compiler : %.2f s" % tc
876 return out
876 return out
877
877
878 @skip_doctest
878 @skip_doctest
879 @line_magic
879 @line_magic
880 def macro(self, parameter_s=''):
880 def macro(self, parameter_s=''):
881 """Define a macro for future re-execution. It accepts ranges of history,
881 """Define a macro for future re-execution. It accepts ranges of history,
882 filenames or string objects.
882 filenames or string objects.
883
883
884 Usage:\\
884 Usage:\\
885 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
885 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
886
886
887 Options:
887 Options:
888
888
889 -r: use 'raw' input. By default, the 'processed' history is used,
889 -r: use 'raw' input. By default, the 'processed' history is used,
890 so that magics are loaded in their transformed version to valid
890 so that magics are loaded in their transformed version to valid
891 Python. If this option is given, the raw input as typed as the
891 Python. If this option is given, the raw input as typed as the
892 command line is used instead.
892 command line is used instead.
893
893
894 This will define a global variable called `name` which is a string
894 This will define a global variable called `name` which is a string
895 made of joining the slices and lines you specify (n1,n2,... numbers
895 made of joining the slices and lines you specify (n1,n2,... numbers
896 above) from your input history into a single string. This variable
896 above) from your input history into a single string. This variable
897 acts like an automatic function which re-executes those lines as if
897 acts like an automatic function which re-executes those lines as if
898 you had typed them. You just type 'name' at the prompt and the code
898 you had typed them. You just type 'name' at the prompt and the code
899 executes.
899 executes.
900
900
901 The syntax for indicating input ranges is described in %history.
901 The syntax for indicating input ranges is described in %history.
902
902
903 Note: as a 'hidden' feature, you can also use traditional python slice
903 Note: as a 'hidden' feature, you can also use traditional python slice
904 notation, where N:M means numbers N through M-1.
904 notation, where N:M means numbers N through M-1.
905
905
906 For example, if your history contains (%hist prints it)::
906 For example, if your history contains (%hist prints it)::
907
907
908 44: x=1
908 44: x=1
909 45: y=3
909 45: y=3
910 46: z=x+y
910 46: z=x+y
911 47: print x
911 47: print x
912 48: a=5
912 48: a=5
913 49: print 'x',x,'y',y
913 49: print 'x',x,'y',y
914
914
915 you can create a macro with lines 44 through 47 (included) and line 49
915 you can create a macro with lines 44 through 47 (included) and line 49
916 called my_macro with::
916 called my_macro with::
917
917
918 In [55]: %macro my_macro 44-47 49
918 In [55]: %macro my_macro 44-47 49
919
919
920 Now, typing `my_macro` (without quotes) will re-execute all this code
920 Now, typing `my_macro` (without quotes) will re-execute all this code
921 in one pass.
921 in one pass.
922
922
923 You don't need to give the line-numbers in order, and any given line
923 You don't need to give the line-numbers in order, and any given line
924 number can appear multiple times. You can assemble macros with any
924 number can appear multiple times. You can assemble macros with any
925 lines from your input history in any order.
925 lines from your input history in any order.
926
926
927 The macro is a simple object which holds its value in an attribute,
927 The macro is a simple object which holds its value in an attribute,
928 but IPython's display system checks for macros and executes them as
928 but IPython's display system checks for macros and executes them as
929 code instead of printing them when you type their name.
929 code instead of printing them when you type their name.
930
930
931 You can view a macro's contents by explicitly printing it with::
931 You can view a macro's contents by explicitly printing it with::
932
932
933 print macro_name
933 print macro_name
934
934
935 """
935 """
936 opts,args = self.parse_options(parameter_s,'r',mode='list')
936 opts,args = self.parse_options(parameter_s,'r',mode='list')
937 if not args: # List existing macros
937 if not args: # List existing macros
938 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
938 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
939 isinstance(v, Macro))
939 isinstance(v, Macro))
940 if len(args) == 1:
940 if len(args) == 1:
941 raise UsageError(
941 raise UsageError(
942 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
942 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
943 name, codefrom = args[0], " ".join(args[1:])
943 name, codefrom = args[0], " ".join(args[1:])
944
944
945 #print 'rng',ranges # dbg
945 #print 'rng',ranges # dbg
946 try:
946 try:
947 lines = self.shell.find_user_code(codefrom, 'r' in opts)
947 lines = self.shell.find_user_code(codefrom, 'r' in opts)
948 except (ValueError, TypeError) as e:
948 except (ValueError, TypeError) as e:
949 print e.args[0]
949 print e.args[0]
950 return
950 return
951 macro = Macro(lines)
951 macro = Macro(lines)
952 self.shell.define_macro(name, macro)
952 self.shell.define_macro(name, macro)
953 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
953 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
954 print '=== Macro contents: ==='
954 print '=== Macro contents: ==='
955 print macro,
955 print macro,
@@ -1,69 +1,69 b''
1 """Implementation of magic functions for the extension machinery.
1 """Implementation of magic functions for the extension machinery.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Stdlib
15 # Stdlib
16 import os
16 import os
17
17
18 # Our own packages
18 # Our own packages
19 from IPython.core.magic import Magics, register_magics, line_magic
19 from IPython.core.magic import Magics, magics_class, line_magic
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Magic implementation classes
22 # Magic implementation classes
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 @register_magics
25 @magics_class
26 class ExtensionMagics(Magics):
26 class ExtensionMagics(Magics):
27 """Magics to manage the IPython extensions system."""
27 """Magics to manage the IPython extensions system."""
28
28
29 @line_magic
29 @line_magic
30 def install_ext(self, parameter_s=''):
30 def install_ext(self, parameter_s=''):
31 """Download and install an extension from a URL, e.g.::
31 """Download and install an extension from a URL, e.g.::
32
32
33 %install_ext https://bitbucket.org/birkenfeld/ipython-physics/raw/d1310a2ab15d/physics.py
33 %install_ext https://bitbucket.org/birkenfeld/ipython-physics/raw/d1310a2ab15d/physics.py
34
34
35 The URL should point to an importable Python module - either a .py file
35 The URL should point to an importable Python module - either a .py file
36 or a .zip file.
36 or a .zip file.
37
37
38 Parameters:
38 Parameters:
39
39
40 -n filename : Specify a name for the file, rather than taking it from
40 -n filename : Specify a name for the file, rather than taking it from
41 the URL.
41 the URL.
42 """
42 """
43 opts, args = self.parse_options(parameter_s, 'n:')
43 opts, args = self.parse_options(parameter_s, 'n:')
44 try:
44 try:
45 filename = self.shell.extension_manager.install_extension(args,
45 filename = self.shell.extension_manager.install_extension(args,
46 opts.get('n'))
46 opts.get('n'))
47 except ValueError as e:
47 except ValueError as e:
48 print e
48 print e
49 return
49 return
50
50
51 filename = os.path.basename(filename)
51 filename = os.path.basename(filename)
52 print "Installed %s. To use it, type:" % filename
52 print "Installed %s. To use it, type:" % filename
53 print " %%load_ext %s" % os.path.splitext(filename)[0]
53 print " %%load_ext %s" % os.path.splitext(filename)[0]
54
54
55
55
56 @line_magic
56 @line_magic
57 def load_ext(self, module_str):
57 def load_ext(self, module_str):
58 """Load an IPython extension by its module name."""
58 """Load an IPython extension by its module name."""
59 return self.shell.extension_manager.load_extension(module_str)
59 return self.shell.extension_manager.load_extension(module_str)
60
60
61 @line_magic
61 @line_magic
62 def unload_ext(self, module_str):
62 def unload_ext(self, module_str):
63 """Unload an IPython extension by its module name."""
63 """Unload an IPython extension by its module name."""
64 self.shell.extension_manager.unload_extension(module_str)
64 self.shell.extension_manager.unload_extension(module_str)
65
65
66 @line_magic
66 @line_magic
67 def reload_ext(self, module_str):
67 def reload_ext(self, module_str):
68 """Reload an IPython extension by its module name."""
68 """Reload an IPython extension by its module name."""
69 self.shell.extension_manager.reload_extension(module_str)
69 self.shell.extension_manager.reload_extension(module_str)
@@ -1,294 +1,294 b''
1 """Implementation of magic functions related to History.
1 """Implementation of magic functions related to History.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012, IPython Development Team.
4 # Copyright (c) 2012, IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 from __future__ import print_function
14 from __future__ import print_function
15
15
16 # Stdlib
16 # Stdlib
17 import os
17 import os
18 from io import open as io_open
18 from io import open as io_open
19
19
20 # Our own packages
20 # Our own packages
21 from IPython.core.error import StdinNotImplementedError
21 from IPython.core.error import StdinNotImplementedError
22 from IPython.core.magic import Magics, register_magics, line_magic
22 from IPython.core.magic import Magics, magics_class, line_magic
23 from IPython.testing.skipdoctest import skip_doctest
23 from IPython.testing.skipdoctest import skip_doctest
24 from IPython.utils import io
24 from IPython.utils import io
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Magics class implementation
27 # Magics class implementation
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 @register_magics
30 @magics_class
31 class HistoryMagics(Magics):
31 class HistoryMagics(Magics):
32
32
33 @skip_doctest
33 @skip_doctest
34 @line_magic
34 @line_magic
35 def history(self, parameter_s = ''):
35 def history(self, parameter_s = ''):
36 """Print input history (_i<n> variables), with most recent last.
36 """Print input history (_i<n> variables), with most recent last.
37
37
38 %history [-o -p -t -n] [-f filename] [range | -g pattern | -l number]
38 %history [-o -p -t -n] [-f filename] [range | -g pattern | -l number]
39
39
40 By default, input history is printed without line numbers so it can be
40 By default, input history is printed without line numbers so it can be
41 directly pasted into an editor. Use -n to show them.
41 directly pasted into an editor. Use -n to show them.
42
42
43 By default, all input history from the current session is displayed.
43 By default, all input history from the current session is displayed.
44 Ranges of history can be indicated using the syntax:
44 Ranges of history can be indicated using the syntax:
45 4 : Line 4, current session
45 4 : Line 4, current session
46 4-6 : Lines 4-6, current session
46 4-6 : Lines 4-6, current session
47 243/1-5: Lines 1-5, session 243
47 243/1-5: Lines 1-5, session 243
48 ~2/7 : Line 7, session 2 before current
48 ~2/7 : Line 7, session 2 before current
49 ~8/1-~6/5 : From the first line of 8 sessions ago, to the fifth line
49 ~8/1-~6/5 : From the first line of 8 sessions ago, to the fifth line
50 of 6 sessions ago.
50 of 6 sessions ago.
51 Multiple ranges can be entered, separated by spaces
51 Multiple ranges can be entered, separated by spaces
52
52
53 The same syntax is used by %macro, %save, %edit, %rerun
53 The same syntax is used by %macro, %save, %edit, %rerun
54
54
55 Options:
55 Options:
56
56
57 -n: print line numbers for each input.
57 -n: print line numbers for each input.
58 This feature is only available if numbered prompts are in use.
58 This feature is only available if numbered prompts are in use.
59
59
60 -o: also print outputs for each input.
60 -o: also print outputs for each input.
61
61
62 -p: print classic '>>>' python prompts before each input. This is
62 -p: print classic '>>>' python prompts before each input. This is
63 useful for making documentation, and in conjunction with -o, for
63 useful for making documentation, and in conjunction with -o, for
64 producing doctest-ready output.
64 producing doctest-ready output.
65
65
66 -r: (default) print the 'raw' history, i.e. the actual commands you
66 -r: (default) print the 'raw' history, i.e. the actual commands you
67 typed.
67 typed.
68
68
69 -t: print the 'translated' history, as IPython understands it.
69 -t: print the 'translated' history, as IPython understands it.
70 IPython filters your input and converts it all into valid Python
70 IPython filters your input and converts it all into valid Python
71 source before executing it (things like magics or aliases are turned
71 source before executing it (things like magics or aliases are turned
72 into function calls, for example). With this option, you'll see the
72 into function calls, for example). With this option, you'll see the
73 native history instead of the user-entered version: '%cd /' will be
73 native history instead of the user-entered version: '%cd /' will be
74 seen as 'get_ipython().magic("%cd /")' instead of '%cd /'.
74 seen as 'get_ipython().magic("%cd /")' instead of '%cd /'.
75
75
76 -g: treat the arg as a pattern to grep for in (full) history.
76 -g: treat the arg as a pattern to grep for in (full) history.
77 This includes the saved history (almost all commands ever written).
77 This includes the saved history (almost all commands ever written).
78 Use '%hist -g' to show full saved history (may be very long).
78 Use '%hist -g' to show full saved history (may be very long).
79
79
80 -l: get the last n lines from all sessions. Specify n as a single
80 -l: get the last n lines from all sessions. Specify n as a single
81 arg, or the default is the last 10 lines.
81 arg, or the default is the last 10 lines.
82
82
83 -f FILENAME: instead of printing the output to the screen, redirect
83 -f FILENAME: instead of printing the output to the screen, redirect
84 it to the given file. The file is always overwritten, though *when
84 it to the given file. The file is always overwritten, though *when
85 it can*, IPython asks for confirmation first. In particular, running
85 it can*, IPython asks for confirmation first. In particular, running
86 the command 'history -f FILENAME' from the IPython Notebook
86 the command 'history -f FILENAME' from the IPython Notebook
87 interface will replace FILENAME even if it already exists *without*
87 interface will replace FILENAME even if it already exists *without*
88 confirmation.
88 confirmation.
89
89
90 Examples
90 Examples
91 --------
91 --------
92 ::
92 ::
93
93
94 In [6]: %hist -n 4-6
94 In [6]: %hist -n 4-6
95 4:a = 12
95 4:a = 12
96 5:print a**2
96 5:print a**2
97 6:%hist -n 4-6
97 6:%hist -n 4-6
98
98
99 """
99 """
100
100
101 if not self.shell.displayhook.do_full_cache:
101 if not self.shell.displayhook.do_full_cache:
102 print('This feature is only available if numbered prompts '
102 print('This feature is only available if numbered prompts '
103 'are in use.')
103 'are in use.')
104 return
104 return
105 opts,args = self.parse_options(parameter_s,'noprtglf:',mode='string')
105 opts,args = self.parse_options(parameter_s,'noprtglf:',mode='string')
106
106
107 # For brevity
107 # For brevity
108 history_manager = self.shell.history_manager
108 history_manager = self.shell.history_manager
109
109
110 def _format_lineno(session, line):
110 def _format_lineno(session, line):
111 """Helper function to format line numbers properly."""
111 """Helper function to format line numbers properly."""
112 if session in (0, history_manager.session_number):
112 if session in (0, history_manager.session_number):
113 return str(line)
113 return str(line)
114 return "%s/%s" % (session, line)
114 return "%s/%s" % (session, line)
115
115
116 # Check if output to specific file was requested.
116 # Check if output to specific file was requested.
117 try:
117 try:
118 outfname = opts['f']
118 outfname = opts['f']
119 except KeyError:
119 except KeyError:
120 outfile = io.stdout # default
120 outfile = io.stdout # default
121 # We don't want to close stdout at the end!
121 # We don't want to close stdout at the end!
122 close_at_end = False
122 close_at_end = False
123 else:
123 else:
124 if os.path.exists(outfname):
124 if os.path.exists(outfname):
125 try:
125 try:
126 ans = io.ask_yes_no("File %r exists. Overwrite?" % outfname)
126 ans = io.ask_yes_no("File %r exists. Overwrite?" % outfname)
127 except StdinNotImplementedError:
127 except StdinNotImplementedError:
128 ans = True
128 ans = True
129 if not ans:
129 if not ans:
130 print('Aborting.')
130 print('Aborting.')
131 return
131 return
132 print("Overwriting file.")
132 print("Overwriting file.")
133 outfile = io_open(outfname, 'w', encoding='utf-8')
133 outfile = io_open(outfname, 'w', encoding='utf-8')
134 close_at_end = True
134 close_at_end = True
135
135
136 print_nums = 'n' in opts
136 print_nums = 'n' in opts
137 get_output = 'o' in opts
137 get_output = 'o' in opts
138 pyprompts = 'p' in opts
138 pyprompts = 'p' in opts
139 # Raw history is the default
139 # Raw history is the default
140 raw = not('t' in opts)
140 raw = not('t' in opts)
141
141
142 pattern = None
142 pattern = None
143
143
144 if 'g' in opts: # Glob search
144 if 'g' in opts: # Glob search
145 pattern = "*" + args + "*" if args else "*"
145 pattern = "*" + args + "*" if args else "*"
146 hist = history_manager.search(pattern, raw=raw, output=get_output)
146 hist = history_manager.search(pattern, raw=raw, output=get_output)
147 print_nums = True
147 print_nums = True
148 elif 'l' in opts: # Get 'tail'
148 elif 'l' in opts: # Get 'tail'
149 try:
149 try:
150 n = int(args)
150 n = int(args)
151 except (ValueError, IndexError):
151 except (ValueError, IndexError):
152 n = 10
152 n = 10
153 hist = history_manager.get_tail(n, raw=raw, output=get_output)
153 hist = history_manager.get_tail(n, raw=raw, output=get_output)
154 else:
154 else:
155 if args: # Get history by ranges
155 if args: # Get history by ranges
156 hist = history_manager.get_range_by_str(args, raw, get_output)
156 hist = history_manager.get_range_by_str(args, raw, get_output)
157 else: # Just get history for the current session
157 else: # Just get history for the current session
158 hist = history_manager.get_range(raw=raw, output=get_output)
158 hist = history_manager.get_range(raw=raw, output=get_output)
159
159
160 # We could be displaying the entire history, so let's not try to pull
160 # We could be displaying the entire history, so let's not try to pull
161 # it into a list in memory. Anything that needs more space will just
161 # it into a list in memory. Anything that needs more space will just
162 # misalign.
162 # misalign.
163 width = 4
163 width = 4
164
164
165 for session, lineno, inline in hist:
165 for session, lineno, inline in hist:
166 # Print user history with tabs expanded to 4 spaces. The GUI
166 # Print user history with tabs expanded to 4 spaces. The GUI
167 # clients use hard tabs for easier usability in auto-indented code,
167 # clients use hard tabs for easier usability in auto-indented code,
168 # but we want to produce PEP-8 compliant history for safe pasting
168 # but we want to produce PEP-8 compliant history for safe pasting
169 # into an editor.
169 # into an editor.
170 if get_output:
170 if get_output:
171 inline, output = inline
171 inline, output = inline
172 inline = inline.expandtabs(4).rstrip()
172 inline = inline.expandtabs(4).rstrip()
173
173
174 multiline = "\n" in inline
174 multiline = "\n" in inline
175 line_sep = '\n' if multiline else ' '
175 line_sep = '\n' if multiline else ' '
176 if print_nums:
176 if print_nums:
177 print(u'%s:%s' % (_format_lineno(session, lineno).rjust(width),
177 print(u'%s:%s' % (_format_lineno(session, lineno).rjust(width),
178 line_sep), file=outfile, end=u'')
178 line_sep), file=outfile, end=u'')
179 if pyprompts:
179 if pyprompts:
180 print(u">>> ", end=u"", file=outfile)
180 print(u">>> ", end=u"", file=outfile)
181 if multiline:
181 if multiline:
182 inline = "\n... ".join(inline.splitlines()) + "\n..."
182 inline = "\n... ".join(inline.splitlines()) + "\n..."
183 print(inline, file=outfile)
183 print(inline, file=outfile)
184 if get_output and output:
184 if get_output and output:
185 print(output, file=outfile)
185 print(output, file=outfile)
186
186
187 if close_at_end:
187 if close_at_end:
188 outfile.close()
188 outfile.close()
189
189
190 # For a long time we've had %hist as well as %history
190 # For a long time we've had %hist as well as %history
191 @line_magic
191 @line_magic
192 def hist(self, arg):
192 def hist(self, arg):
193 return self.history(arg)
193 return self.history(arg)
194
194
195 hist.__doc__ = history.__doc__
195 hist.__doc__ = history.__doc__
196
196
197 @line_magic
197 @line_magic
198 def rep(self, arg):
198 def rep(self, arg):
199 r"""Repeat a command, or get command to input line for editing.
199 r"""Repeat a command, or get command to input line for editing.
200
200
201 %recall and %rep are equivalent.
201 %recall and %rep are equivalent.
202
202
203 - %recall (no arguments):
203 - %recall (no arguments):
204
204
205 Place a string version of last computation result (stored in the
205 Place a string version of last computation result (stored in the
206 special '_' variable) to the next input prompt. Allows you to create
206 special '_' variable) to the next input prompt. Allows you to create
207 elaborate command lines without using copy-paste::
207 elaborate command lines without using copy-paste::
208
208
209 In[1]: l = ["hei", "vaan"]
209 In[1]: l = ["hei", "vaan"]
210 In[2]: "".join(l)
210 In[2]: "".join(l)
211 Out[2]: heivaan
211 Out[2]: heivaan
212 In[3]: %rep
212 In[3]: %rep
213 In[4]: heivaan_ <== cursor blinking
213 In[4]: heivaan_ <== cursor blinking
214
214
215 %recall 45
215 %recall 45
216
216
217 Place history line 45 on the next input prompt. Use %hist to find
217 Place history line 45 on the next input prompt. Use %hist to find
218 out the number.
218 out the number.
219
219
220 %recall 1-4
220 %recall 1-4
221
221
222 Combine the specified lines into one cell, and place it on the next
222 Combine the specified lines into one cell, and place it on the next
223 input prompt. See %history for the slice syntax.
223 input prompt. See %history for the slice syntax.
224
224
225 %recall foo+bar
225 %recall foo+bar
226
226
227 If foo+bar can be evaluated in the user namespace, the result is
227 If foo+bar can be evaluated in the user namespace, the result is
228 placed at the next input prompt. Otherwise, the history is searched
228 placed at the next input prompt. Otherwise, the history is searched
229 for lines which contain that substring, and the most recent one is
229 for lines which contain that substring, and the most recent one is
230 placed at the next input prompt.
230 placed at the next input prompt.
231 """
231 """
232 if not arg: # Last output
232 if not arg: # Last output
233 self.shell.set_next_input(str(self.shell.user_ns["_"]))
233 self.shell.set_next_input(str(self.shell.user_ns["_"]))
234 return
234 return
235 # Get history range
235 # Get history range
236 histlines = self.shell.history_manager.get_range_by_str(arg)
236 histlines = self.shell.history_manager.get_range_by_str(arg)
237 cmd = "\n".join(x[2] for x in histlines)
237 cmd = "\n".join(x[2] for x in histlines)
238 if cmd:
238 if cmd:
239 self.shell.set_next_input(cmd.rstrip())
239 self.shell.set_next_input(cmd.rstrip())
240 return
240 return
241
241
242 try: # Variable in user namespace
242 try: # Variable in user namespace
243 cmd = str(eval(arg, self.shell.user_ns))
243 cmd = str(eval(arg, self.shell.user_ns))
244 except Exception: # Search for term in history
244 except Exception: # Search for term in history
245 histlines = self.shell.history_manager.search("*"+arg+"*")
245 histlines = self.shell.history_manager.search("*"+arg+"*")
246 for h in reversed([x[2] for x in histlines]):
246 for h in reversed([x[2] for x in histlines]):
247 if 'rep' in h:
247 if 'rep' in h:
248 continue
248 continue
249 self.shell.set_next_input(h.rstrip())
249 self.shell.set_next_input(h.rstrip())
250 return
250 return
251 else:
251 else:
252 self.shell.set_next_input(cmd.rstrip())
252 self.shell.set_next_input(cmd.rstrip())
253 print("Couldn't evaluate or find in history:", arg)
253 print("Couldn't evaluate or find in history:", arg)
254
254
255 @line_magic
255 @line_magic
256 def rerun(self, parameter_s=''):
256 def rerun(self, parameter_s=''):
257 """Re-run previous input
257 """Re-run previous input
258
258
259 By default, you can specify ranges of input history to be repeated
259 By default, you can specify ranges of input history to be repeated
260 (as with %history). With no arguments, it will repeat the last line.
260 (as with %history). With no arguments, it will repeat the last line.
261
261
262 Options:
262 Options:
263
263
264 -l <n> : Repeat the last n lines of input, not including the
264 -l <n> : Repeat the last n lines of input, not including the
265 current command.
265 current command.
266
266
267 -g foo : Repeat the most recent line which contains foo
267 -g foo : Repeat the most recent line which contains foo
268 """
268 """
269 opts, args = self.parse_options(parameter_s, 'l:g:', mode='string')
269 opts, args = self.parse_options(parameter_s, 'l:g:', mode='string')
270 if "l" in opts: # Last n lines
270 if "l" in opts: # Last n lines
271 n = int(opts['l'])
271 n = int(opts['l'])
272 hist = self.shell.history_manager.get_tail(n)
272 hist = self.shell.history_manager.get_tail(n)
273 elif "g" in opts: # Search
273 elif "g" in opts: # Search
274 p = "*"+opts['g']+"*"
274 p = "*"+opts['g']+"*"
275 hist = list(self.shell.history_manager.search(p))
275 hist = list(self.shell.history_manager.search(p))
276 for l in reversed(hist):
276 for l in reversed(hist):
277 if "rerun" not in l[2]:
277 if "rerun" not in l[2]:
278 hist = [l] # The last match which isn't a %rerun
278 hist = [l] # The last match which isn't a %rerun
279 break
279 break
280 else:
280 else:
281 hist = [] # No matches except %rerun
281 hist = [] # No matches except %rerun
282 elif args: # Specify history ranges
282 elif args: # Specify history ranges
283 hist = self.shell.history_manager.get_range_by_str(args)
283 hist = self.shell.history_manager.get_range_by_str(args)
284 else: # Last line
284 else: # Last line
285 hist = self.shell.history_manager.get_tail(1)
285 hist = self.shell.history_manager.get_tail(1)
286 hist = [x[2] for x in hist]
286 hist = [x[2] for x in hist]
287 if not hist:
287 if not hist:
288 print("No lines in history match specification")
288 print("No lines in history match specification")
289 return
289 return
290 histlines = "\n".join(hist)
290 histlines = "\n".join(hist)
291 print("=== Executing: ===")
291 print("=== Executing: ===")
292 print(histlines)
292 print(histlines)
293 print("=== Output: ===")
293 print("=== Output: ===")
294 self.shell.run_cell("\n".join(hist), store_history=False)
294 self.shell.run_cell("\n".join(hist), store_history=False)
@@ -1,169 +1,169 b''
1 """Implementation of magic functions for IPython's own logging.
1 """Implementation of magic functions for IPython's own logging.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Stdlib
15 # Stdlib
16 import os
16 import os
17 import sys
17 import sys
18
18
19 # Our own packages
19 # Our own packages
20 from IPython.core.magic import Magics, register_magics, line_magic
20 from IPython.core.magic import Magics, magics_class, line_magic
21 from IPython.utils.warn import warn
21 from IPython.utils.warn import warn
22
22
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24 # Magic implementation classes
24 # Magic implementation classes
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
26
27 @register_magics
27 @magics_class
28 class LoggingMagics(Magics):
28 class LoggingMagics(Magics):
29 """Magics related to all logging machinery."""
29 """Magics related to all logging machinery."""
30
30
31 @line_magic
31 @line_magic
32 def logstart(self, parameter_s=''):
32 def logstart(self, parameter_s=''):
33 """Start logging anywhere in a session.
33 """Start logging anywhere in a session.
34
34
35 %logstart [-o|-r|-t] [log_name [log_mode]]
35 %logstart [-o|-r|-t] [log_name [log_mode]]
36
36
37 If no name is given, it defaults to a file named 'ipython_log.py' in your
37 If no name is given, it defaults to a file named 'ipython_log.py' in your
38 current directory, in 'rotate' mode (see below).
38 current directory, in 'rotate' mode (see below).
39
39
40 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
40 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
41 history up to that point and then continues logging.
41 history up to that point and then continues logging.
42
42
43 %logstart takes a second optional parameter: logging mode. This can be one
43 %logstart takes a second optional parameter: logging mode. This can be one
44 of (note that the modes are given unquoted):\\
44 of (note that the modes are given unquoted):\\
45 append: well, that says it.\\
45 append: well, that says it.\\
46 backup: rename (if exists) to name~ and start name.\\
46 backup: rename (if exists) to name~ and start name.\\
47 global: single logfile in your home dir, appended to.\\
47 global: single logfile in your home dir, appended to.\\
48 over : overwrite existing log.\\
48 over : overwrite existing log.\\
49 rotate: create rotating logs name.1~, name.2~, etc.
49 rotate: create rotating logs name.1~, name.2~, etc.
50
50
51 Options:
51 Options:
52
52
53 -o: log also IPython's output. In this mode, all commands which
53 -o: log also IPython's output. In this mode, all commands which
54 generate an Out[NN] prompt are recorded to the logfile, right after
54 generate an Out[NN] prompt are recorded to the logfile, right after
55 their corresponding input line. The output lines are always
55 their corresponding input line. The output lines are always
56 prepended with a '#[Out]# ' marker, so that the log remains valid
56 prepended with a '#[Out]# ' marker, so that the log remains valid
57 Python code.
57 Python code.
58
58
59 Since this marker is always the same, filtering only the output from
59 Since this marker is always the same, filtering only the output from
60 a log is very easy, using for example a simple awk call::
60 a log is very easy, using for example a simple awk call::
61
61
62 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
62 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
63
63
64 -r: log 'raw' input. Normally, IPython's logs contain the processed
64 -r: log 'raw' input. Normally, IPython's logs contain the processed
65 input, so that user lines are logged in their final form, converted
65 input, so that user lines are logged in their final form, converted
66 into valid Python. For example, %Exit is logged as
66 into valid Python. For example, %Exit is logged as
67 _ip.magic("Exit"). If the -r flag is given, all input is logged
67 _ip.magic("Exit"). If the -r flag is given, all input is logged
68 exactly as typed, with no transformations applied.
68 exactly as typed, with no transformations applied.
69
69
70 -t: put timestamps before each input line logged (these are put in
70 -t: put timestamps before each input line logged (these are put in
71 comments)."""
71 comments)."""
72
72
73 opts,par = self.parse_options(parameter_s,'ort')
73 opts,par = self.parse_options(parameter_s,'ort')
74 log_output = 'o' in opts
74 log_output = 'o' in opts
75 log_raw_input = 'r' in opts
75 log_raw_input = 'r' in opts
76 timestamp = 't' in opts
76 timestamp = 't' in opts
77
77
78 logger = self.shell.logger
78 logger = self.shell.logger
79
79
80 # if no args are given, the defaults set in the logger constructor by
80 # if no args are given, the defaults set in the logger constructor by
81 # ipython remain valid
81 # ipython remain valid
82 if par:
82 if par:
83 try:
83 try:
84 logfname,logmode = par.split()
84 logfname,logmode = par.split()
85 except:
85 except:
86 logfname = par
86 logfname = par
87 logmode = 'backup'
87 logmode = 'backup'
88 else:
88 else:
89 logfname = logger.logfname
89 logfname = logger.logfname
90 logmode = logger.logmode
90 logmode = logger.logmode
91 # put logfname into rc struct as if it had been called on the command
91 # put logfname into rc struct as if it had been called on the command
92 # line, so it ends up saved in the log header Save it in case we need
92 # line, so it ends up saved in the log header Save it in case we need
93 # to restore it...
93 # to restore it...
94 old_logfile = self.shell.logfile
94 old_logfile = self.shell.logfile
95 if logfname:
95 if logfname:
96 logfname = os.path.expanduser(logfname)
96 logfname = os.path.expanduser(logfname)
97 self.shell.logfile = logfname
97 self.shell.logfile = logfname
98
98
99 loghead = '# IPython log file\n\n'
99 loghead = '# IPython log file\n\n'
100 try:
100 try:
101 logger.logstart(logfname, loghead, logmode, log_output, timestamp,
101 logger.logstart(logfname, loghead, logmode, log_output, timestamp,
102 log_raw_input)
102 log_raw_input)
103 except:
103 except:
104 self.shell.logfile = old_logfile
104 self.shell.logfile = old_logfile
105 warn("Couldn't start log: %s" % sys.exc_info()[1])
105 warn("Couldn't start log: %s" % sys.exc_info()[1])
106 else:
106 else:
107 # log input history up to this point, optionally interleaving
107 # log input history up to this point, optionally interleaving
108 # output if requested
108 # output if requested
109
109
110 if timestamp:
110 if timestamp:
111 # disable timestamping for the previous history, since we've
111 # disable timestamping for the previous history, since we've
112 # lost those already (no time machine here).
112 # lost those already (no time machine here).
113 logger.timestamp = False
113 logger.timestamp = False
114
114
115 if log_raw_input:
115 if log_raw_input:
116 input_hist = self.shell.history_manager.input_hist_raw
116 input_hist = self.shell.history_manager.input_hist_raw
117 else:
117 else:
118 input_hist = self.shell.history_manager.input_hist_parsed
118 input_hist = self.shell.history_manager.input_hist_parsed
119
119
120 if log_output:
120 if log_output:
121 log_write = logger.log_write
121 log_write = logger.log_write
122 output_hist = self.shell.history_manager.output_hist
122 output_hist = self.shell.history_manager.output_hist
123 for n in range(1,len(input_hist)-1):
123 for n in range(1,len(input_hist)-1):
124 log_write(input_hist[n].rstrip() + '\n')
124 log_write(input_hist[n].rstrip() + '\n')
125 if n in output_hist:
125 if n in output_hist:
126 log_write(repr(output_hist[n]),'output')
126 log_write(repr(output_hist[n]),'output')
127 else:
127 else:
128 logger.log_write('\n'.join(input_hist[1:]))
128 logger.log_write('\n'.join(input_hist[1:]))
129 logger.log_write('\n')
129 logger.log_write('\n')
130 if timestamp:
130 if timestamp:
131 # re-enable timestamping
131 # re-enable timestamping
132 logger.timestamp = True
132 logger.timestamp = True
133
133
134 print ('Activating auto-logging. '
134 print ('Activating auto-logging. '
135 'Current session state plus future input saved.')
135 'Current session state plus future input saved.')
136 logger.logstate()
136 logger.logstate()
137
137
138 @line_magic
138 @line_magic
139 def logstop(self, parameter_s=''):
139 def logstop(self, parameter_s=''):
140 """Fully stop logging and close log file.
140 """Fully stop logging and close log file.
141
141
142 In order to start logging again, a new %logstart call needs to be made,
142 In order to start logging again, a new %logstart call needs to be made,
143 possibly (though not necessarily) with a new filename, mode and other
143 possibly (though not necessarily) with a new filename, mode and other
144 options."""
144 options."""
145 self.logger.logstop()
145 self.logger.logstop()
146
146
147 @line_magic
147 @line_magic
148 def logoff(self, parameter_s=''):
148 def logoff(self, parameter_s=''):
149 """Temporarily stop logging.
149 """Temporarily stop logging.
150
150
151 You must have previously started logging."""
151 You must have previously started logging."""
152 self.shell.logger.switch_log(0)
152 self.shell.logger.switch_log(0)
153
153
154 @line_magic
154 @line_magic
155 def logon(self, parameter_s=''):
155 def logon(self, parameter_s=''):
156 """Restart logging.
156 """Restart logging.
157
157
158 This function is for restarting logging which you've temporarily
158 This function is for restarting logging which you've temporarily
159 stopped with %logoff. For starting logging for the first time, you
159 stopped with %logoff. For starting logging for the first time, you
160 must use the %logstart function, which allows you to specify an
160 must use the %logstart function, which allows you to specify an
161 optional log filename."""
161 optional log filename."""
162
162
163 self.shell.logger.switch_log(1)
163 self.shell.logger.switch_log(1)
164
164
165 @line_magic
165 @line_magic
166 def logstate(self, parameter_s=''):
166 def logstate(self, parameter_s=''):
167 """Print the status of the logging system."""
167 """Print the status of the logging system."""
168
168
169 self.shell.logger.logstate()
169 self.shell.logger.logstate()
@@ -1,702 +1,702 b''
1 """Implementation of namespace-related magic functions.
1 """Implementation of namespace-related magic functions.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Stdlib
15 # Stdlib
16 import gc
16 import gc
17 import re
17 import re
18 import sys
18 import sys
19
19
20 # Our own packages
20 # Our own packages
21 from IPython.core import page
21 from IPython.core import page
22 from IPython.core.error import StdinNotImplementedError
22 from IPython.core.error import StdinNotImplementedError
23 from IPython.core.magic import Magics, register_magics, line_magic
23 from IPython.core.magic import Magics, magics_class, line_magic
24 from IPython.testing.skipdoctest import skip_doctest
24 from IPython.testing.skipdoctest import skip_doctest
25 from IPython.utils.encoding import DEFAULT_ENCODING
25 from IPython.utils.encoding import DEFAULT_ENCODING
26 from IPython.utils.path import get_py_filename
26 from IPython.utils.path import get_py_filename
27
27
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 # Magic implementation classes
29 # Magic implementation classes
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31
31
32 @register_magics
32 @magics_class
33 class NamespaceMagics(Magics):
33 class NamespaceMagics(Magics):
34 """Magics to manage various aspects of the user's namespace.
34 """Magics to manage various aspects of the user's namespace.
35
35
36 These include listing variables, introspecting into them, etc.
36 These include listing variables, introspecting into them, etc.
37 """
37 """
38
38
39 @line_magic
39 @line_magic
40 def pinfo(self, parameter_s='', namespaces=None):
40 def pinfo(self, parameter_s='', namespaces=None):
41 """Provide detailed information about an object.
41 """Provide detailed information about an object.
42
42
43 '%pinfo object' is just a synonym for object? or ?object."""
43 '%pinfo object' is just a synonym for object? or ?object."""
44
44
45 #print 'pinfo par: <%s>' % parameter_s # dbg
45 #print 'pinfo par: <%s>' % parameter_s # dbg
46
46
47
47
48 # detail_level: 0 -> obj? , 1 -> obj??
48 # detail_level: 0 -> obj? , 1 -> obj??
49 detail_level = 0
49 detail_level = 0
50 # We need to detect if we got called as 'pinfo pinfo foo', which can
50 # We need to detect if we got called as 'pinfo pinfo foo', which can
51 # happen if the user types 'pinfo foo?' at the cmd line.
51 # happen if the user types 'pinfo foo?' at the cmd line.
52 pinfo,qmark1,oname,qmark2 = \
52 pinfo,qmark1,oname,qmark2 = \
53 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
53 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
54 if pinfo or qmark1 or qmark2:
54 if pinfo or qmark1 or qmark2:
55 detail_level = 1
55 detail_level = 1
56 if "*" in oname:
56 if "*" in oname:
57 self.psearch(oname)
57 self.psearch(oname)
58 else:
58 else:
59 self.shell._inspect('pinfo', oname, detail_level=detail_level,
59 self.shell._inspect('pinfo', oname, detail_level=detail_level,
60 namespaces=namespaces)
60 namespaces=namespaces)
61
61
62 @line_magic
62 @line_magic
63 def pinfo2(self, parameter_s='', namespaces=None):
63 def pinfo2(self, parameter_s='', namespaces=None):
64 """Provide extra detailed information about an object.
64 """Provide extra detailed information about an object.
65
65
66 '%pinfo2 object' is just a synonym for object?? or ??object."""
66 '%pinfo2 object' is just a synonym for object?? or ??object."""
67 self.shell._inspect('pinfo', parameter_s, detail_level=1,
67 self.shell._inspect('pinfo', parameter_s, detail_level=1,
68 namespaces=namespaces)
68 namespaces=namespaces)
69
69
70 @skip_doctest
70 @skip_doctest
71 @line_magic
71 @line_magic
72 def pdef(self, parameter_s='', namespaces=None):
72 def pdef(self, parameter_s='', namespaces=None):
73 """Print the definition header for any callable object.
73 """Print the definition header for any callable object.
74
74
75 If the object is a class, print the constructor information.
75 If the object is a class, print the constructor information.
76
76
77 Examples
77 Examples
78 --------
78 --------
79 ::
79 ::
80
80
81 In [3]: %pdef urllib.urlopen
81 In [3]: %pdef urllib.urlopen
82 urllib.urlopen(url, data=None, proxies=None)
82 urllib.urlopen(url, data=None, proxies=None)
83 """
83 """
84 self._inspect('pdef',parameter_s, namespaces)
84 self._inspect('pdef',parameter_s, namespaces)
85
85
86 @line_magic
86 @line_magic
87 def pdoc(self, parameter_s='', namespaces=None):
87 def pdoc(self, parameter_s='', namespaces=None):
88 """Print the docstring for an object.
88 """Print the docstring for an object.
89
89
90 If the given object is a class, it will print both the class and the
90 If the given object is a class, it will print both the class and the
91 constructor docstrings."""
91 constructor docstrings."""
92 self._inspect('pdoc',parameter_s, namespaces)
92 self._inspect('pdoc',parameter_s, namespaces)
93
93
94 @line_magic
94 @line_magic
95 def psource(self, parameter_s='', namespaces=None):
95 def psource(self, parameter_s='', namespaces=None):
96 """Print (or run through pager) the source code for an object."""
96 """Print (or run through pager) the source code for an object."""
97 self._inspect('psource',parameter_s, namespaces)
97 self._inspect('psource',parameter_s, namespaces)
98
98
99 @line_magic
99 @line_magic
100 def pfile(self, parameter_s=''):
100 def pfile(self, parameter_s=''):
101 """Print (or run through pager) the file where an object is defined.
101 """Print (or run through pager) the file where an object is defined.
102
102
103 The file opens at the line where the object definition begins. IPython
103 The file opens at the line where the object definition begins. IPython
104 will honor the environment variable PAGER if set, and otherwise will
104 will honor the environment variable PAGER if set, and otherwise will
105 do its best to print the file in a convenient form.
105 do its best to print the file in a convenient form.
106
106
107 If the given argument is not an object currently defined, IPython will
107 If the given argument is not an object currently defined, IPython will
108 try to interpret it as a filename (automatically adding a .py extension
108 try to interpret it as a filename (automatically adding a .py extension
109 if needed). You can thus use %pfile as a syntax highlighting code
109 if needed). You can thus use %pfile as a syntax highlighting code
110 viewer."""
110 viewer."""
111
111
112 # first interpret argument as an object name
112 # first interpret argument as an object name
113 out = self._inspect('pfile',parameter_s)
113 out = self._inspect('pfile',parameter_s)
114 # if not, try the input as a filename
114 # if not, try the input as a filename
115 if out == 'not found':
115 if out == 'not found':
116 try:
116 try:
117 filename = get_py_filename(parameter_s)
117 filename = get_py_filename(parameter_s)
118 except IOError,msg:
118 except IOError,msg:
119 print msg
119 print msg
120 return
120 return
121 page.page(self.shell.inspector.format(open(filename).read()))
121 page.page(self.shell.inspector.format(open(filename).read()))
122
122
123 @line_magic
123 @line_magic
124 def psearch(self, parameter_s=''):
124 def psearch(self, parameter_s=''):
125 """Search for object in namespaces by wildcard.
125 """Search for object in namespaces by wildcard.
126
126
127 %psearch [options] PATTERN [OBJECT TYPE]
127 %psearch [options] PATTERN [OBJECT TYPE]
128
128
129 Note: ? can be used as a synonym for %psearch, at the beginning or at
129 Note: ? can be used as a synonym for %psearch, at the beginning or at
130 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
130 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
131 rest of the command line must be unchanged (options come first), so
131 rest of the command line must be unchanged (options come first), so
132 for example the following forms are equivalent
132 for example the following forms are equivalent
133
133
134 %psearch -i a* function
134 %psearch -i a* function
135 -i a* function?
135 -i a* function?
136 ?-i a* function
136 ?-i a* function
137
137
138 Arguments:
138 Arguments:
139
139
140 PATTERN
140 PATTERN
141
141
142 where PATTERN is a string containing * as a wildcard similar to its
142 where PATTERN is a string containing * as a wildcard similar to its
143 use in a shell. The pattern is matched in all namespaces on the
143 use in a shell. The pattern is matched in all namespaces on the
144 search path. By default objects starting with a single _ are not
144 search path. By default objects starting with a single _ are not
145 matched, many IPython generated objects have a single
145 matched, many IPython generated objects have a single
146 underscore. The default is case insensitive matching. Matching is
146 underscore. The default is case insensitive matching. Matching is
147 also done on the attributes of objects and not only on the objects
147 also done on the attributes of objects and not only on the objects
148 in a module.
148 in a module.
149
149
150 [OBJECT TYPE]
150 [OBJECT TYPE]
151
151
152 Is the name of a python type from the types module. The name is
152 Is the name of a python type from the types module. The name is
153 given in lowercase without the ending type, ex. StringType is
153 given in lowercase without the ending type, ex. StringType is
154 written string. By adding a type here only objects matching the
154 written string. By adding a type here only objects matching the
155 given type are matched. Using all here makes the pattern match all
155 given type are matched. Using all here makes the pattern match all
156 types (this is the default).
156 types (this is the default).
157
157
158 Options:
158 Options:
159
159
160 -a: makes the pattern match even objects whose names start with a
160 -a: makes the pattern match even objects whose names start with a
161 single underscore. These names are normally omitted from the
161 single underscore. These names are normally omitted from the
162 search.
162 search.
163
163
164 -i/-c: make the pattern case insensitive/sensitive. If neither of
164 -i/-c: make the pattern case insensitive/sensitive. If neither of
165 these options are given, the default is read from your configuration
165 these options are given, the default is read from your configuration
166 file, with the option ``InteractiveShell.wildcards_case_sensitive``.
166 file, with the option ``InteractiveShell.wildcards_case_sensitive``.
167 If this option is not specified in your configuration file, IPython's
167 If this option is not specified in your configuration file, IPython's
168 internal default is to do a case sensitive search.
168 internal default is to do a case sensitive search.
169
169
170 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
170 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
171 specify can be searched in any of the following namespaces:
171 specify can be searched in any of the following namespaces:
172 'builtin', 'user', 'user_global','internal', 'alias', where
172 'builtin', 'user', 'user_global','internal', 'alias', where
173 'builtin' and 'user' are the search defaults. Note that you should
173 'builtin' and 'user' are the search defaults. Note that you should
174 not use quotes when specifying namespaces.
174 not use quotes when specifying namespaces.
175
175
176 'Builtin' contains the python module builtin, 'user' contains all
176 'Builtin' contains the python module builtin, 'user' contains all
177 user data, 'alias' only contain the shell aliases and no python
177 user data, 'alias' only contain the shell aliases and no python
178 objects, 'internal' contains objects used by IPython. The
178 objects, 'internal' contains objects used by IPython. The
179 'user_global' namespace is only used by embedded IPython instances,
179 'user_global' namespace is only used by embedded IPython instances,
180 and it contains module-level globals. You can add namespaces to the
180 and it contains module-level globals. You can add namespaces to the
181 search with -s or exclude them with -e (these options can be given
181 search with -s or exclude them with -e (these options can be given
182 more than once).
182 more than once).
183
183
184 Examples
184 Examples
185 --------
185 --------
186 ::
186 ::
187
187
188 %psearch a* -> objects beginning with an a
188 %psearch a* -> objects beginning with an a
189 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
189 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
190 %psearch a* function -> all functions beginning with an a
190 %psearch a* function -> all functions beginning with an a
191 %psearch re.e* -> objects beginning with an e in module re
191 %psearch re.e* -> objects beginning with an e in module re
192 %psearch r*.e* -> objects that start with e in modules starting in r
192 %psearch r*.e* -> objects that start with e in modules starting in r
193 %psearch r*.* string -> all strings in modules beginning with r
193 %psearch r*.* string -> all strings in modules beginning with r
194
194
195 Case sensitive search::
195 Case sensitive search::
196
196
197 %psearch -c a* list all object beginning with lower case a
197 %psearch -c a* list all object beginning with lower case a
198
198
199 Show objects beginning with a single _::
199 Show objects beginning with a single _::
200
200
201 %psearch -a _* list objects beginning with a single underscore
201 %psearch -a _* list objects beginning with a single underscore
202 """
202 """
203 try:
203 try:
204 parameter_s.encode('ascii')
204 parameter_s.encode('ascii')
205 except UnicodeEncodeError:
205 except UnicodeEncodeError:
206 print 'Python identifiers can only contain ascii characters.'
206 print 'Python identifiers can only contain ascii characters.'
207 return
207 return
208
208
209 # default namespaces to be searched
209 # default namespaces to be searched
210 def_search = ['user_local', 'user_global', 'builtin']
210 def_search = ['user_local', 'user_global', 'builtin']
211
211
212 # Process options/args
212 # Process options/args
213 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
213 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
214 opt = opts.get
214 opt = opts.get
215 shell = self.shell
215 shell = self.shell
216 psearch = shell.inspector.psearch
216 psearch = shell.inspector.psearch
217
217
218 # select case options
218 # select case options
219 if opts.has_key('i'):
219 if opts.has_key('i'):
220 ignore_case = True
220 ignore_case = True
221 elif opts.has_key('c'):
221 elif opts.has_key('c'):
222 ignore_case = False
222 ignore_case = False
223 else:
223 else:
224 ignore_case = not shell.wildcards_case_sensitive
224 ignore_case = not shell.wildcards_case_sensitive
225
225
226 # Build list of namespaces to search from user options
226 # Build list of namespaces to search from user options
227 def_search.extend(opt('s',[]))
227 def_search.extend(opt('s',[]))
228 ns_exclude = ns_exclude=opt('e',[])
228 ns_exclude = ns_exclude=opt('e',[])
229 ns_search = [nm for nm in def_search if nm not in ns_exclude]
229 ns_search = [nm for nm in def_search if nm not in ns_exclude]
230
230
231 # Call the actual search
231 # Call the actual search
232 try:
232 try:
233 psearch(args,shell.ns_table,ns_search,
233 psearch(args,shell.ns_table,ns_search,
234 show_all=opt('a'),ignore_case=ignore_case)
234 show_all=opt('a'),ignore_case=ignore_case)
235 except:
235 except:
236 shell.showtraceback()
236 shell.showtraceback()
237
237
238 @skip_doctest
238 @skip_doctest
239 @line_magic
239 @line_magic
240 def who_ls(self, parameter_s=''):
240 def who_ls(self, parameter_s=''):
241 """Return a sorted list of all interactive variables.
241 """Return a sorted list of all interactive variables.
242
242
243 If arguments are given, only variables of types matching these
243 If arguments are given, only variables of types matching these
244 arguments are returned.
244 arguments are returned.
245
245
246 Examples
246 Examples
247 --------
247 --------
248
248
249 Define two variables and list them with who_ls::
249 Define two variables and list them with who_ls::
250
250
251 In [1]: alpha = 123
251 In [1]: alpha = 123
252
252
253 In [2]: beta = 'test'
253 In [2]: beta = 'test'
254
254
255 In [3]: %who_ls
255 In [3]: %who_ls
256 Out[3]: ['alpha', 'beta']
256 Out[3]: ['alpha', 'beta']
257
257
258 In [4]: %who_ls int
258 In [4]: %who_ls int
259 Out[4]: ['alpha']
259 Out[4]: ['alpha']
260
260
261 In [5]: %who_ls str
261 In [5]: %who_ls str
262 Out[5]: ['beta']
262 Out[5]: ['beta']
263 """
263 """
264
264
265 user_ns = self.shell.user_ns
265 user_ns = self.shell.user_ns
266 user_ns_hidden = self.shell.user_ns_hidden
266 user_ns_hidden = self.shell.user_ns_hidden
267 out = [ i for i in user_ns
267 out = [ i for i in user_ns
268 if not i.startswith('_') \
268 if not i.startswith('_') \
269 and not i in user_ns_hidden ]
269 and not i in user_ns_hidden ]
270
270
271 typelist = parameter_s.split()
271 typelist = parameter_s.split()
272 if typelist:
272 if typelist:
273 typeset = set(typelist)
273 typeset = set(typelist)
274 out = [i for i in out if type(user_ns[i]).__name__ in typeset]
274 out = [i for i in out if type(user_ns[i]).__name__ in typeset]
275
275
276 out.sort()
276 out.sort()
277 return out
277 return out
278
278
279 @skip_doctest
279 @skip_doctest
280 @line_magic
280 @line_magic
281 def who(self, parameter_s=''):
281 def who(self, parameter_s=''):
282 """Print all interactive variables, with some minimal formatting.
282 """Print all interactive variables, with some minimal formatting.
283
283
284 If any arguments are given, only variables whose type matches one of
284 If any arguments are given, only variables whose type matches one of
285 these are printed. For example::
285 these are printed. For example::
286
286
287 %who function str
287 %who function str
288
288
289 will only list functions and strings, excluding all other types of
289 will only list functions and strings, excluding all other types of
290 variables. To find the proper type names, simply use type(var) at a
290 variables. To find the proper type names, simply use type(var) at a
291 command line to see how python prints type names. For example:
291 command line to see how python prints type names. For example:
292
292
293 ::
293 ::
294
294
295 In [1]: type('hello')\\
295 In [1]: type('hello')\\
296 Out[1]: <type 'str'>
296 Out[1]: <type 'str'>
297
297
298 indicates that the type name for strings is 'str'.
298 indicates that the type name for strings is 'str'.
299
299
300 ``%who`` always excludes executed names loaded through your configuration
300 ``%who`` always excludes executed names loaded through your configuration
301 file and things which are internal to IPython.
301 file and things which are internal to IPython.
302
302
303 This is deliberate, as typically you may load many modules and the
303 This is deliberate, as typically you may load many modules and the
304 purpose of %who is to show you only what you've manually defined.
304 purpose of %who is to show you only what you've manually defined.
305
305
306 Examples
306 Examples
307 --------
307 --------
308
308
309 Define two variables and list them with who::
309 Define two variables and list them with who::
310
310
311 In [1]: alpha = 123
311 In [1]: alpha = 123
312
312
313 In [2]: beta = 'test'
313 In [2]: beta = 'test'
314
314
315 In [3]: %who
315 In [3]: %who
316 alpha beta
316 alpha beta
317
317
318 In [4]: %who int
318 In [4]: %who int
319 alpha
319 alpha
320
320
321 In [5]: %who str
321 In [5]: %who str
322 beta
322 beta
323 """
323 """
324
324
325 varlist = self.who_ls(parameter_s)
325 varlist = self.who_ls(parameter_s)
326 if not varlist:
326 if not varlist:
327 if parameter_s:
327 if parameter_s:
328 print 'No variables match your requested type.'
328 print 'No variables match your requested type.'
329 else:
329 else:
330 print 'Interactive namespace is empty.'
330 print 'Interactive namespace is empty.'
331 return
331 return
332
332
333 # if we have variables, move on...
333 # if we have variables, move on...
334 count = 0
334 count = 0
335 for i in varlist:
335 for i in varlist:
336 print i+'\t',
336 print i+'\t',
337 count += 1
337 count += 1
338 if count > 8:
338 if count > 8:
339 count = 0
339 count = 0
340 print
340 print
341 print
341 print
342
342
343 @skip_doctest
343 @skip_doctest
344 @line_magic
344 @line_magic
345 def whos(self, parameter_s=''):
345 def whos(self, parameter_s=''):
346 """Like %who, but gives some extra information about each variable.
346 """Like %who, but gives some extra information about each variable.
347
347
348 The same type filtering of %who can be applied here.
348 The same type filtering of %who can be applied here.
349
349
350 For all variables, the type is printed. Additionally it prints:
350 For all variables, the type is printed. Additionally it prints:
351
351
352 - For {},[],(): their length.
352 - For {},[],(): their length.
353
353
354 - For numpy arrays, a summary with shape, number of
354 - For numpy arrays, a summary with shape, number of
355 elements, typecode and size in memory.
355 elements, typecode and size in memory.
356
356
357 - Everything else: a string representation, snipping their middle if
357 - Everything else: a string representation, snipping their middle if
358 too long.
358 too long.
359
359
360 Examples
360 Examples
361 --------
361 --------
362
362
363 Define two variables and list them with whos::
363 Define two variables and list them with whos::
364
364
365 In [1]: alpha = 123
365 In [1]: alpha = 123
366
366
367 In [2]: beta = 'test'
367 In [2]: beta = 'test'
368
368
369 In [3]: %whos
369 In [3]: %whos
370 Variable Type Data/Info
370 Variable Type Data/Info
371 --------------------------------
371 --------------------------------
372 alpha int 123
372 alpha int 123
373 beta str test
373 beta str test
374 """
374 """
375
375
376 varnames = self.who_ls(parameter_s)
376 varnames = self.who_ls(parameter_s)
377 if not varnames:
377 if not varnames:
378 if parameter_s:
378 if parameter_s:
379 print 'No variables match your requested type.'
379 print 'No variables match your requested type.'
380 else:
380 else:
381 print 'Interactive namespace is empty.'
381 print 'Interactive namespace is empty.'
382 return
382 return
383
383
384 # if we have variables, move on...
384 # if we have variables, move on...
385
385
386 # for these types, show len() instead of data:
386 # for these types, show len() instead of data:
387 seq_types = ['dict', 'list', 'tuple']
387 seq_types = ['dict', 'list', 'tuple']
388
388
389 # for numpy arrays, display summary info
389 # for numpy arrays, display summary info
390 ndarray_type = None
390 ndarray_type = None
391 if 'numpy' in sys.modules:
391 if 'numpy' in sys.modules:
392 try:
392 try:
393 from numpy import ndarray
393 from numpy import ndarray
394 except ImportError:
394 except ImportError:
395 pass
395 pass
396 else:
396 else:
397 ndarray_type = ndarray.__name__
397 ndarray_type = ndarray.__name__
398
398
399 # Find all variable names and types so we can figure out column sizes
399 # Find all variable names and types so we can figure out column sizes
400 def get_vars(i):
400 def get_vars(i):
401 return self.shell.user_ns[i]
401 return self.shell.user_ns[i]
402
402
403 # some types are well known and can be shorter
403 # some types are well known and can be shorter
404 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
404 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
405 def type_name(v):
405 def type_name(v):
406 tn = type(v).__name__
406 tn = type(v).__name__
407 return abbrevs.get(tn,tn)
407 return abbrevs.get(tn,tn)
408
408
409 varlist = map(get_vars,varnames)
409 varlist = map(get_vars,varnames)
410
410
411 typelist = []
411 typelist = []
412 for vv in varlist:
412 for vv in varlist:
413 tt = type_name(vv)
413 tt = type_name(vv)
414
414
415 if tt=='instance':
415 if tt=='instance':
416 typelist.append( abbrevs.get(str(vv.__class__),
416 typelist.append( abbrevs.get(str(vv.__class__),
417 str(vv.__class__)))
417 str(vv.__class__)))
418 else:
418 else:
419 typelist.append(tt)
419 typelist.append(tt)
420
420
421 # column labels and # of spaces as separator
421 # column labels and # of spaces as separator
422 varlabel = 'Variable'
422 varlabel = 'Variable'
423 typelabel = 'Type'
423 typelabel = 'Type'
424 datalabel = 'Data/Info'
424 datalabel = 'Data/Info'
425 colsep = 3
425 colsep = 3
426 # variable format strings
426 # variable format strings
427 vformat = "{0:<{varwidth}}{1:<{typewidth}}"
427 vformat = "{0:<{varwidth}}{1:<{typewidth}}"
428 aformat = "%s: %s elems, type `%s`, %s bytes"
428 aformat = "%s: %s elems, type `%s`, %s bytes"
429 # find the size of the columns to format the output nicely
429 # find the size of the columns to format the output nicely
430 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
430 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
431 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
431 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
432 # table header
432 # table header
433 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
433 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
434 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
434 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
435 # and the table itself
435 # and the table itself
436 kb = 1024
436 kb = 1024
437 Mb = 1048576 # kb**2
437 Mb = 1048576 # kb**2
438 for vname,var,vtype in zip(varnames,varlist,typelist):
438 for vname,var,vtype in zip(varnames,varlist,typelist):
439 print vformat.format(vname, vtype, varwidth=varwidth, typewidth=typewidth),
439 print vformat.format(vname, vtype, varwidth=varwidth, typewidth=typewidth),
440 if vtype in seq_types:
440 if vtype in seq_types:
441 print "n="+str(len(var))
441 print "n="+str(len(var))
442 elif vtype == ndarray_type:
442 elif vtype == ndarray_type:
443 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
443 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
444 if vtype==ndarray_type:
444 if vtype==ndarray_type:
445 # numpy
445 # numpy
446 vsize = var.size
446 vsize = var.size
447 vbytes = vsize*var.itemsize
447 vbytes = vsize*var.itemsize
448 vdtype = var.dtype
448 vdtype = var.dtype
449
449
450 if vbytes < 100000:
450 if vbytes < 100000:
451 print aformat % (vshape,vsize,vdtype,vbytes)
451 print aformat % (vshape,vsize,vdtype,vbytes)
452 else:
452 else:
453 print aformat % (vshape,vsize,vdtype,vbytes),
453 print aformat % (vshape,vsize,vdtype,vbytes),
454 if vbytes < Mb:
454 if vbytes < Mb:
455 print '(%s kb)' % (vbytes/kb,)
455 print '(%s kb)' % (vbytes/kb,)
456 else:
456 else:
457 print '(%s Mb)' % (vbytes/Mb,)
457 print '(%s Mb)' % (vbytes/Mb,)
458 else:
458 else:
459 try:
459 try:
460 vstr = str(var)
460 vstr = str(var)
461 except UnicodeEncodeError:
461 except UnicodeEncodeError:
462 vstr = unicode(var).encode(DEFAULT_ENCODING,
462 vstr = unicode(var).encode(DEFAULT_ENCODING,
463 'backslashreplace')
463 'backslashreplace')
464 except:
464 except:
465 vstr = "<object with id %d (str() failed)>" % id(var)
465 vstr = "<object with id %d (str() failed)>" % id(var)
466 vstr = vstr.replace('\n','\\n')
466 vstr = vstr.replace('\n','\\n')
467 if len(vstr) < 50:
467 if len(vstr) < 50:
468 print vstr
468 print vstr
469 else:
469 else:
470 print vstr[:25] + "<...>" + vstr[-25:]
470 print vstr[:25] + "<...>" + vstr[-25:]
471
471
472 @line_magic
472 @line_magic
473 def reset(self, parameter_s=''):
473 def reset(self, parameter_s=''):
474 """Resets the namespace by removing all names defined by the user, if
474 """Resets the namespace by removing all names defined by the user, if
475 called without arguments, or by removing some types of objects, such
475 called without arguments, or by removing some types of objects, such
476 as everything currently in IPython's In[] and Out[] containers (see
476 as everything currently in IPython's In[] and Out[] containers (see
477 the parameters for details).
477 the parameters for details).
478
478
479 Parameters
479 Parameters
480 ----------
480 ----------
481 -f : force reset without asking for confirmation.
481 -f : force reset without asking for confirmation.
482
482
483 -s : 'Soft' reset: Only clears your namespace, leaving history intact.
483 -s : 'Soft' reset: Only clears your namespace, leaving history intact.
484 References to objects may be kept. By default (without this option),
484 References to objects may be kept. By default (without this option),
485 we do a 'hard' reset, giving you a new session and removing all
485 we do a 'hard' reset, giving you a new session and removing all
486 references to objects from the current session.
486 references to objects from the current session.
487
487
488 in : reset input history
488 in : reset input history
489
489
490 out : reset output history
490 out : reset output history
491
491
492 dhist : reset directory history
492 dhist : reset directory history
493
493
494 array : reset only variables that are NumPy arrays
494 array : reset only variables that are NumPy arrays
495
495
496 See Also
496 See Also
497 --------
497 --------
498 magic_reset_selective : invoked as ``%reset_selective``
498 magic_reset_selective : invoked as ``%reset_selective``
499
499
500 Examples
500 Examples
501 --------
501 --------
502 ::
502 ::
503
503
504 In [6]: a = 1
504 In [6]: a = 1
505
505
506 In [7]: a
506 In [7]: a
507 Out[7]: 1
507 Out[7]: 1
508
508
509 In [8]: 'a' in _ip.user_ns
509 In [8]: 'a' in _ip.user_ns
510 Out[8]: True
510 Out[8]: True
511
511
512 In [9]: %reset -f
512 In [9]: %reset -f
513
513
514 In [1]: 'a' in _ip.user_ns
514 In [1]: 'a' in _ip.user_ns
515 Out[1]: False
515 Out[1]: False
516
516
517 In [2]: %reset -f in
517 In [2]: %reset -f in
518 Flushing input history
518 Flushing input history
519
519
520 In [3]: %reset -f dhist in
520 In [3]: %reset -f dhist in
521 Flushing directory history
521 Flushing directory history
522 Flushing input history
522 Flushing input history
523
523
524 Notes
524 Notes
525 -----
525 -----
526 Calling this magic from clients that do not implement standard input,
526 Calling this magic from clients that do not implement standard input,
527 such as the ipython notebook interface, will reset the namespace
527 such as the ipython notebook interface, will reset the namespace
528 without confirmation.
528 without confirmation.
529 """
529 """
530 opts, args = self.parse_options(parameter_s,'sf', mode='list')
530 opts, args = self.parse_options(parameter_s,'sf', mode='list')
531 if 'f' in opts:
531 if 'f' in opts:
532 ans = True
532 ans = True
533 else:
533 else:
534 try:
534 try:
535 ans = self.shell.ask_yes_no(
535 ans = self.shell.ask_yes_no(
536 "Once deleted, variables cannot be recovered. Proceed (y/[n])?",
536 "Once deleted, variables cannot be recovered. Proceed (y/[n])?",
537 default='n')
537 default='n')
538 except StdinNotImplementedError:
538 except StdinNotImplementedError:
539 ans = True
539 ans = True
540 if not ans:
540 if not ans:
541 print 'Nothing done.'
541 print 'Nothing done.'
542 return
542 return
543
543
544 if 's' in opts: # Soft reset
544 if 's' in opts: # Soft reset
545 user_ns = self.shell.user_ns
545 user_ns = self.shell.user_ns
546 for i in self.who_ls():
546 for i in self.who_ls():
547 del(user_ns[i])
547 del(user_ns[i])
548 elif len(args) == 0: # Hard reset
548 elif len(args) == 0: # Hard reset
549 self.shell.reset(new_session = False)
549 self.shell.reset(new_session = False)
550
550
551 # reset in/out/dhist/array: previously extensinions/clearcmd.py
551 # reset in/out/dhist/array: previously extensinions/clearcmd.py
552 ip = self.shell
552 ip = self.shell
553 user_ns = self.shell.user_ns # local lookup, heavily used
553 user_ns = self.shell.user_ns # local lookup, heavily used
554
554
555 for target in args:
555 for target in args:
556 target = target.lower() # make matches case insensitive
556 target = target.lower() # make matches case insensitive
557 if target == 'out':
557 if target == 'out':
558 print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
558 print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
559 self.shell.displayhook.flush()
559 self.shell.displayhook.flush()
560
560
561 elif target == 'in':
561 elif target == 'in':
562 print "Flushing input history"
562 print "Flushing input history"
563 pc = self.shell.displayhook.prompt_count + 1
563 pc = self.shell.displayhook.prompt_count + 1
564 for n in range(1, pc):
564 for n in range(1, pc):
565 key = '_i'+repr(n)
565 key = '_i'+repr(n)
566 user_ns.pop(key,None)
566 user_ns.pop(key,None)
567 user_ns.update(dict(_i=u'',_ii=u'',_iii=u''))
567 user_ns.update(dict(_i=u'',_ii=u'',_iii=u''))
568 hm = ip.history_manager
568 hm = ip.history_manager
569 # don't delete these, as %save and %macro depending on the
569 # don't delete these, as %save and %macro depending on the
570 # length of these lists to be preserved
570 # length of these lists to be preserved
571 hm.input_hist_parsed[:] = [''] * pc
571 hm.input_hist_parsed[:] = [''] * pc
572 hm.input_hist_raw[:] = [''] * pc
572 hm.input_hist_raw[:] = [''] * pc
573 # hm has internal machinery for _i,_ii,_iii, clear it out
573 # hm has internal machinery for _i,_ii,_iii, clear it out
574 hm._i = hm._ii = hm._iii = hm._i00 = u''
574 hm._i = hm._ii = hm._iii = hm._i00 = u''
575
575
576 elif target == 'array':
576 elif target == 'array':
577 # Support cleaning up numpy arrays
577 # Support cleaning up numpy arrays
578 try:
578 try:
579 from numpy import ndarray
579 from numpy import ndarray
580 # This must be done with items and not iteritems because
580 # This must be done with items and not iteritems because
581 # we're going to modify the dict in-place.
581 # we're going to modify the dict in-place.
582 for x,val in user_ns.items():
582 for x,val in user_ns.items():
583 if isinstance(val,ndarray):
583 if isinstance(val,ndarray):
584 del user_ns[x]
584 del user_ns[x]
585 except ImportError:
585 except ImportError:
586 print "reset array only works if Numpy is available."
586 print "reset array only works if Numpy is available."
587
587
588 elif target == 'dhist':
588 elif target == 'dhist':
589 print "Flushing directory history"
589 print "Flushing directory history"
590 del user_ns['_dh'][:]
590 del user_ns['_dh'][:]
591
591
592 else:
592 else:
593 print "Don't know how to reset ",
593 print "Don't know how to reset ",
594 print target + ", please run `%reset?` for details"
594 print target + ", please run `%reset?` for details"
595
595
596 gc.collect()
596 gc.collect()
597
597
598 @line_magic
598 @line_magic
599 def reset_selective(self, parameter_s=''):
599 def reset_selective(self, parameter_s=''):
600 """Resets the namespace by removing names defined by the user.
600 """Resets the namespace by removing names defined by the user.
601
601
602 Input/Output history are left around in case you need them.
602 Input/Output history are left around in case you need them.
603
603
604 %reset_selective [-f] regex
604 %reset_selective [-f] regex
605
605
606 No action is taken if regex is not included
606 No action is taken if regex is not included
607
607
608 Options
608 Options
609 -f : force reset without asking for confirmation.
609 -f : force reset without asking for confirmation.
610
610
611 See Also
611 See Also
612 --------
612 --------
613 magic_reset : invoked as ``%reset``
613 magic_reset : invoked as ``%reset``
614
614
615 Examples
615 Examples
616 --------
616 --------
617
617
618 We first fully reset the namespace so your output looks identical to
618 We first fully reset the namespace so your output looks identical to
619 this example for pedagogical reasons; in practice you do not need a
619 this example for pedagogical reasons; in practice you do not need a
620 full reset::
620 full reset::
621
621
622 In [1]: %reset -f
622 In [1]: %reset -f
623
623
624 Now, with a clean namespace we can make a few variables and use
624 Now, with a clean namespace we can make a few variables and use
625 ``%reset_selective`` to only delete names that match our regexp::
625 ``%reset_selective`` to only delete names that match our regexp::
626
626
627 In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
627 In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
628
628
629 In [3]: who_ls
629 In [3]: who_ls
630 Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
630 Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
631
631
632 In [4]: %reset_selective -f b[2-3]m
632 In [4]: %reset_selective -f b[2-3]m
633
633
634 In [5]: who_ls
634 In [5]: who_ls
635 Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
635 Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
636
636
637 In [6]: %reset_selective -f d
637 In [6]: %reset_selective -f d
638
638
639 In [7]: who_ls
639 In [7]: who_ls
640 Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
640 Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
641
641
642 In [8]: %reset_selective -f c
642 In [8]: %reset_selective -f c
643
643
644 In [9]: who_ls
644 In [9]: who_ls
645 Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']
645 Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']
646
646
647 In [10]: %reset_selective -f b
647 In [10]: %reset_selective -f b
648
648
649 In [11]: who_ls
649 In [11]: who_ls
650 Out[11]: ['a']
650 Out[11]: ['a']
651
651
652 Notes
652 Notes
653 -----
653 -----
654 Calling this magic from clients that do not implement standard input,
654 Calling this magic from clients that do not implement standard input,
655 such as the ipython notebook interface, will reset the namespace
655 such as the ipython notebook interface, will reset the namespace
656 without confirmation.
656 without confirmation.
657 """
657 """
658
658
659 opts, regex = self.parse_options(parameter_s,'f')
659 opts, regex = self.parse_options(parameter_s,'f')
660
660
661 if opts.has_key('f'):
661 if opts.has_key('f'):
662 ans = True
662 ans = True
663 else:
663 else:
664 try:
664 try:
665 ans = self.shell.ask_yes_no(
665 ans = self.shell.ask_yes_no(
666 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ",
666 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ",
667 default='n')
667 default='n')
668 except StdinNotImplementedError:
668 except StdinNotImplementedError:
669 ans = True
669 ans = True
670 if not ans:
670 if not ans:
671 print 'Nothing done.'
671 print 'Nothing done.'
672 return
672 return
673 user_ns = self.shell.user_ns
673 user_ns = self.shell.user_ns
674 if not regex:
674 if not regex:
675 print 'No regex pattern specified. Nothing done.'
675 print 'No regex pattern specified. Nothing done.'
676 return
676 return
677 else:
677 else:
678 try:
678 try:
679 m = re.compile(regex)
679 m = re.compile(regex)
680 except TypeError:
680 except TypeError:
681 raise TypeError('regex must be a string or compiled pattern')
681 raise TypeError('regex must be a string or compiled pattern')
682 for i in self.who_ls():
682 for i in self.who_ls():
683 if m.search(i):
683 if m.search(i):
684 del(user_ns[i])
684 del(user_ns[i])
685
685
686 @line_magic
686 @line_magic
687 def xdel(self, parameter_s=''):
687 def xdel(self, parameter_s=''):
688 """Delete a variable, trying to clear it from anywhere that
688 """Delete a variable, trying to clear it from anywhere that
689 IPython's machinery has references to it. By default, this uses
689 IPython's machinery has references to it. By default, this uses
690 the identity of the named object in the user namespace to remove
690 the identity of the named object in the user namespace to remove
691 references held under other names. The object is also removed
691 references held under other names. The object is also removed
692 from the output history.
692 from the output history.
693
693
694 Options
694 Options
695 -n : Delete the specified name from all namespaces, without
695 -n : Delete the specified name from all namespaces, without
696 checking their identity.
696 checking their identity.
697 """
697 """
698 opts, varname = self.parse_options(parameter_s,'n')
698 opts, varname = self.parse_options(parameter_s,'n')
699 try:
699 try:
700 self.shell.del_var(varname, ('n' in opts))
700 self.shell.del_var(varname, ('n' in opts))
701 except (NameError, ValueError) as e:
701 except (NameError, ValueError) as e:
702 print type(e).__name__ +": "+ str(e)
702 print type(e).__name__ +": "+ str(e)
@@ -1,676 +1,676 b''
1 """Implementation of magic functions for interaction with the OS.
1 """Implementation of magic functions for interaction with the OS.
2
2
3 Note: this module is named 'osm' instead of 'os' to avoid a collision with the
3 Note: this module is named 'osm' instead of 'os' to avoid a collision with the
4 builtin.
4 builtin.
5 """
5 """
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2012 The IPython Development Team.
7 # Copyright (c) 2012 The IPython Development Team.
8 #
8 #
9 # Distributed under the terms of the Modified BSD License.
9 # Distributed under the terms of the Modified BSD License.
10 #
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 # Stdlib
18 # Stdlib
19 import os
19 import os
20 import re
20 import re
21 import sys
21 import sys
22 from pprint import pformat
22 from pprint import pformat
23
23
24 # Our own packages
24 # Our own packages
25 from IPython.core import oinspect
25 from IPython.core import oinspect
26 from IPython.core import page
26 from IPython.core import page
27 from IPython.core.error import UsageError
27 from IPython.core.error import UsageError
28 from IPython.core.magic import (Magics, compress_dhist, register_magics,
28 from IPython.core.magic import (Magics, compress_dhist, magics_class,
29 line_magic)
29 line_magic)
30 from IPython.testing.skipdoctest import skip_doctest
30 from IPython.testing.skipdoctest import skip_doctest
31 from IPython.utils.io import file_read, nlprint
31 from IPython.utils.io import file_read, nlprint
32 from IPython.utils.path import get_py_filename, unquote_filename
32 from IPython.utils.path import get_py_filename, unquote_filename
33 from IPython.utils.process import abbrev_cwd
33 from IPython.utils.process import abbrev_cwd
34 from IPython.utils.terminal import set_term_title
34 from IPython.utils.terminal import set_term_title
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36 # Magic implementation classes
36 # Magic implementation classes
37 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
38 @register_magics
38 @magics_class
39 class OSMagics(Magics):
39 class OSMagics(Magics):
40 """Magics to interact with the underlying OS (shell-type functionality).
40 """Magics to interact with the underlying OS (shell-type functionality).
41 """
41 """
42
42
43 @skip_doctest
43 @skip_doctest
44 @line_magic
44 @line_magic
45 def alias(self, parameter_s=''):
45 def alias(self, parameter_s=''):
46 """Define an alias for a system command.
46 """Define an alias for a system command.
47
47
48 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
48 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
49
49
50 Then, typing 'alias_name params' will execute the system command 'cmd
50 Then, typing 'alias_name params' will execute the system command 'cmd
51 params' (from your underlying operating system).
51 params' (from your underlying operating system).
52
52
53 Aliases have lower precedence than magic functions and Python normal
53 Aliases have lower precedence than magic functions and Python normal
54 variables, so if 'foo' is both a Python variable and an alias, the
54 variables, so if 'foo' is both a Python variable and an alias, the
55 alias can not be executed until 'del foo' removes the Python variable.
55 alias can not be executed until 'del foo' removes the Python variable.
56
56
57 You can use the %l specifier in an alias definition to represent the
57 You can use the %l specifier in an alias definition to represent the
58 whole line when the alias is called. For example::
58 whole line when the alias is called. For example::
59
59
60 In [2]: alias bracket echo "Input in brackets: <%l>"
60 In [2]: alias bracket echo "Input in brackets: <%l>"
61 In [3]: bracket hello world
61 In [3]: bracket hello world
62 Input in brackets: <hello world>
62 Input in brackets: <hello world>
63
63
64 You can also define aliases with parameters using %s specifiers (one
64 You can also define aliases with parameters using %s specifiers (one
65 per parameter)::
65 per parameter)::
66
66
67 In [1]: alias parts echo first %s second %s
67 In [1]: alias parts echo first %s second %s
68 In [2]: %parts A B
68 In [2]: %parts A B
69 first A second B
69 first A second B
70 In [3]: %parts A
70 In [3]: %parts A
71 Incorrect number of arguments: 2 expected.
71 Incorrect number of arguments: 2 expected.
72 parts is an alias to: 'echo first %s second %s'
72 parts is an alias to: 'echo first %s second %s'
73
73
74 Note that %l and %s are mutually exclusive. You can only use one or
74 Note that %l and %s are mutually exclusive. You can only use one or
75 the other in your aliases.
75 the other in your aliases.
76
76
77 Aliases expand Python variables just like system calls using ! or !!
77 Aliases expand Python variables just like system calls using ! or !!
78 do: all expressions prefixed with '$' get expanded. For details of
78 do: all expressions prefixed with '$' get expanded. For details of
79 the semantic rules, see PEP-215:
79 the semantic rules, see PEP-215:
80 http://www.python.org/peps/pep-0215.html. This is the library used by
80 http://www.python.org/peps/pep-0215.html. This is the library used by
81 IPython for variable expansion. If you want to access a true shell
81 IPython for variable expansion. If you want to access a true shell
82 variable, an extra $ is necessary to prevent its expansion by
82 variable, an extra $ is necessary to prevent its expansion by
83 IPython::
83 IPython::
84
84
85 In [6]: alias show echo
85 In [6]: alias show echo
86 In [7]: PATH='A Python string'
86 In [7]: PATH='A Python string'
87 In [8]: show $PATH
87 In [8]: show $PATH
88 A Python string
88 A Python string
89 In [9]: show $$PATH
89 In [9]: show $$PATH
90 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
90 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
91
91
92 You can use the alias facility to acess all of $PATH. See the %rehash
92 You can use the alias facility to acess all of $PATH. See the %rehash
93 and %rehashx functions, which automatically create aliases for the
93 and %rehashx functions, which automatically create aliases for the
94 contents of your $PATH.
94 contents of your $PATH.
95
95
96 If called with no parameters, %alias prints the current alias table."""
96 If called with no parameters, %alias prints the current alias table."""
97
97
98 par = parameter_s.strip()
98 par = parameter_s.strip()
99 if not par:
99 if not par:
100 aliases = sorted(self.shell.alias_manager.aliases)
100 aliases = sorted(self.shell.alias_manager.aliases)
101 # stored = self.shell.db.get('stored_aliases', {} )
101 # stored = self.shell.db.get('stored_aliases', {} )
102 # for k, v in stored:
102 # for k, v in stored:
103 # atab.append(k, v[0])
103 # atab.append(k, v[0])
104
104
105 print "Total number of aliases:", len(aliases)
105 print "Total number of aliases:", len(aliases)
106 sys.stdout.flush()
106 sys.stdout.flush()
107 return aliases
107 return aliases
108
108
109 # Now try to define a new one
109 # Now try to define a new one
110 try:
110 try:
111 alias,cmd = par.split(None, 1)
111 alias,cmd = par.split(None, 1)
112 except:
112 except:
113 print oinspect.getdoc(self.alias)
113 print oinspect.getdoc(self.alias)
114 else:
114 else:
115 self.shell.alias_manager.soft_define_alias(alias, cmd)
115 self.shell.alias_manager.soft_define_alias(alias, cmd)
116 # end magic_alias
116 # end magic_alias
117
117
118 @line_magic
118 @line_magic
119 def unalias(self, parameter_s=''):
119 def unalias(self, parameter_s=''):
120 """Remove an alias"""
120 """Remove an alias"""
121
121
122 aname = parameter_s.strip()
122 aname = parameter_s.strip()
123 self.shell.alias_manager.undefine_alias(aname)
123 self.shell.alias_manager.undefine_alias(aname)
124 stored = self.shell.db.get('stored_aliases', {} )
124 stored = self.shell.db.get('stored_aliases', {} )
125 if aname in stored:
125 if aname in stored:
126 print "Removing %stored alias",aname
126 print "Removing %stored alias",aname
127 del stored[aname]
127 del stored[aname]
128 self.shell.db['stored_aliases'] = stored
128 self.shell.db['stored_aliases'] = stored
129
129
130 @line_magic
130 @line_magic
131 def rehashx(self, parameter_s=''):
131 def rehashx(self, parameter_s=''):
132 """Update the alias table with all executable files in $PATH.
132 """Update the alias table with all executable files in $PATH.
133
133
134 This version explicitly checks that every entry in $PATH is a file
134 This version explicitly checks that every entry in $PATH is a file
135 with execute access (os.X_OK), so it is much slower than %rehash.
135 with execute access (os.X_OK), so it is much slower than %rehash.
136
136
137 Under Windows, it checks executability as a match against a
137 Under Windows, it checks executability as a match against a
138 '|'-separated string of extensions, stored in the IPython config
138 '|'-separated string of extensions, stored in the IPython config
139 variable win_exec_ext. This defaults to 'exe|com|bat'.
139 variable win_exec_ext. This defaults to 'exe|com|bat'.
140
140
141 This function also resets the root module cache of module completer,
141 This function also resets the root module cache of module completer,
142 used on slow filesystems.
142 used on slow filesystems.
143 """
143 """
144 from IPython.core.alias import InvalidAliasError
144 from IPython.core.alias import InvalidAliasError
145
145
146 # for the benefit of module completer in ipy_completers.py
146 # for the benefit of module completer in ipy_completers.py
147 del self.shell.db['rootmodules']
147 del self.shell.db['rootmodules']
148
148
149 path = [os.path.abspath(os.path.expanduser(p)) for p in
149 path = [os.path.abspath(os.path.expanduser(p)) for p in
150 os.environ.get('PATH','').split(os.pathsep)]
150 os.environ.get('PATH','').split(os.pathsep)]
151 path = filter(os.path.isdir,path)
151 path = filter(os.path.isdir,path)
152
152
153 syscmdlist = []
153 syscmdlist = []
154 # Now define isexec in a cross platform manner.
154 # Now define isexec in a cross platform manner.
155 if os.name == 'posix':
155 if os.name == 'posix':
156 isexec = lambda fname:os.path.isfile(fname) and \
156 isexec = lambda fname:os.path.isfile(fname) and \
157 os.access(fname,os.X_OK)
157 os.access(fname,os.X_OK)
158 else:
158 else:
159 try:
159 try:
160 winext = os.environ['pathext'].replace(';','|').replace('.','')
160 winext = os.environ['pathext'].replace(';','|').replace('.','')
161 except KeyError:
161 except KeyError:
162 winext = 'exe|com|bat|py'
162 winext = 'exe|com|bat|py'
163 if 'py' not in winext:
163 if 'py' not in winext:
164 winext += '|py'
164 winext += '|py'
165 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
165 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
166 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
166 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
167 savedir = os.getcwdu()
167 savedir = os.getcwdu()
168
168
169 # Now walk the paths looking for executables to alias.
169 # Now walk the paths looking for executables to alias.
170 try:
170 try:
171 # write the whole loop for posix/Windows so we don't have an if in
171 # write the whole loop for posix/Windows so we don't have an if in
172 # the innermost part
172 # the innermost part
173 if os.name == 'posix':
173 if os.name == 'posix':
174 for pdir in path:
174 for pdir in path:
175 os.chdir(pdir)
175 os.chdir(pdir)
176 for ff in os.listdir(pdir):
176 for ff in os.listdir(pdir):
177 if isexec(ff):
177 if isexec(ff):
178 try:
178 try:
179 # Removes dots from the name since ipython
179 # Removes dots from the name since ipython
180 # will assume names with dots to be python.
180 # will assume names with dots to be python.
181 self.shell.alias_manager.define_alias(
181 self.shell.alias_manager.define_alias(
182 ff.replace('.',''), ff)
182 ff.replace('.',''), ff)
183 except InvalidAliasError:
183 except InvalidAliasError:
184 pass
184 pass
185 else:
185 else:
186 syscmdlist.append(ff)
186 syscmdlist.append(ff)
187 else:
187 else:
188 no_alias = self.shell.alias_manager.no_alias
188 no_alias = self.shell.alias_manager.no_alias
189 for pdir in path:
189 for pdir in path:
190 os.chdir(pdir)
190 os.chdir(pdir)
191 for ff in os.listdir(pdir):
191 for ff in os.listdir(pdir):
192 base, ext = os.path.splitext(ff)
192 base, ext = os.path.splitext(ff)
193 if isexec(ff) and base.lower() not in no_alias:
193 if isexec(ff) and base.lower() not in no_alias:
194 if ext.lower() == '.exe':
194 if ext.lower() == '.exe':
195 ff = base
195 ff = base
196 try:
196 try:
197 # Removes dots from the name since ipython
197 # Removes dots from the name since ipython
198 # will assume names with dots to be python.
198 # will assume names with dots to be python.
199 self.shell.alias_manager.define_alias(
199 self.shell.alias_manager.define_alias(
200 base.lower().replace('.',''), ff)
200 base.lower().replace('.',''), ff)
201 except InvalidAliasError:
201 except InvalidAliasError:
202 pass
202 pass
203 syscmdlist.append(ff)
203 syscmdlist.append(ff)
204 self.shell.db['syscmdlist'] = syscmdlist
204 self.shell.db['syscmdlist'] = syscmdlist
205 finally:
205 finally:
206 os.chdir(savedir)
206 os.chdir(savedir)
207
207
208 @skip_doctest
208 @skip_doctest
209 @line_magic
209 @line_magic
210 def pwd(self, parameter_s=''):
210 def pwd(self, parameter_s=''):
211 """Return the current working directory path.
211 """Return the current working directory path.
212
212
213 Examples
213 Examples
214 --------
214 --------
215 ::
215 ::
216
216
217 In [9]: pwd
217 In [9]: pwd
218 Out[9]: '/home/tsuser/sprint/ipython'
218 Out[9]: '/home/tsuser/sprint/ipython'
219 """
219 """
220 return os.getcwdu()
220 return os.getcwdu()
221
221
222 @skip_doctest
222 @skip_doctest
223 @line_magic
223 @line_magic
224 def cd(self, parameter_s=''):
224 def cd(self, parameter_s=''):
225 """Change the current working directory.
225 """Change the current working directory.
226
226
227 This command automatically maintains an internal list of directories
227 This command automatically maintains an internal list of directories
228 you visit during your IPython session, in the variable _dh. The
228 you visit during your IPython session, in the variable _dh. The
229 command %dhist shows this history nicely formatted. You can also
229 command %dhist shows this history nicely formatted. You can also
230 do 'cd -<tab>' to see directory history conveniently.
230 do 'cd -<tab>' to see directory history conveniently.
231
231
232 Usage:
232 Usage:
233
233
234 cd 'dir': changes to directory 'dir'.
234 cd 'dir': changes to directory 'dir'.
235
235
236 cd -: changes to the last visited directory.
236 cd -: changes to the last visited directory.
237
237
238 cd -<n>: changes to the n-th directory in the directory history.
238 cd -<n>: changes to the n-th directory in the directory history.
239
239
240 cd --foo: change to directory that matches 'foo' in history
240 cd --foo: change to directory that matches 'foo' in history
241
241
242 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
242 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
243 (note: cd <bookmark_name> is enough if there is no
243 (note: cd <bookmark_name> is enough if there is no
244 directory <bookmark_name>, but a bookmark with the name exists.)
244 directory <bookmark_name>, but a bookmark with the name exists.)
245 'cd -b <tab>' allows you to tab-complete bookmark names.
245 'cd -b <tab>' allows you to tab-complete bookmark names.
246
246
247 Options:
247 Options:
248
248
249 -q: quiet. Do not print the working directory after the cd command is
249 -q: quiet. Do not print the working directory after the cd command is
250 executed. By default IPython's cd command does print this directory,
250 executed. By default IPython's cd command does print this directory,
251 since the default prompts do not display path information.
251 since the default prompts do not display path information.
252
252
253 Note that !cd doesn't work for this purpose because the shell where
253 Note that !cd doesn't work for this purpose because the shell where
254 !command runs is immediately discarded after executing 'command'.
254 !command runs is immediately discarded after executing 'command'.
255
255
256 Examples
256 Examples
257 --------
257 --------
258 ::
258 ::
259
259
260 In [10]: cd parent/child
260 In [10]: cd parent/child
261 /home/tsuser/parent/child
261 /home/tsuser/parent/child
262 """
262 """
263
263
264 #bkms = self.shell.persist.get("bookmarks",{})
264 #bkms = self.shell.persist.get("bookmarks",{})
265
265
266 oldcwd = os.getcwdu()
266 oldcwd = os.getcwdu()
267 numcd = re.match(r'(-)(\d+)$',parameter_s)
267 numcd = re.match(r'(-)(\d+)$',parameter_s)
268 # jump in directory history by number
268 # jump in directory history by number
269 if numcd:
269 if numcd:
270 nn = int(numcd.group(2))
270 nn = int(numcd.group(2))
271 try:
271 try:
272 ps = self.shell.user_ns['_dh'][nn]
272 ps = self.shell.user_ns['_dh'][nn]
273 except IndexError:
273 except IndexError:
274 print 'The requested directory does not exist in history.'
274 print 'The requested directory does not exist in history.'
275 return
275 return
276 else:
276 else:
277 opts = {}
277 opts = {}
278 elif parameter_s.startswith('--'):
278 elif parameter_s.startswith('--'):
279 ps = None
279 ps = None
280 fallback = None
280 fallback = None
281 pat = parameter_s[2:]
281 pat = parameter_s[2:]
282 dh = self.shell.user_ns['_dh']
282 dh = self.shell.user_ns['_dh']
283 # first search only by basename (last component)
283 # first search only by basename (last component)
284 for ent in reversed(dh):
284 for ent in reversed(dh):
285 if pat in os.path.basename(ent) and os.path.isdir(ent):
285 if pat in os.path.basename(ent) and os.path.isdir(ent):
286 ps = ent
286 ps = ent
287 break
287 break
288
288
289 if fallback is None and pat in ent and os.path.isdir(ent):
289 if fallback is None and pat in ent and os.path.isdir(ent):
290 fallback = ent
290 fallback = ent
291
291
292 # if we have no last part match, pick the first full path match
292 # if we have no last part match, pick the first full path match
293 if ps is None:
293 if ps is None:
294 ps = fallback
294 ps = fallback
295
295
296 if ps is None:
296 if ps is None:
297 print "No matching entry in directory history"
297 print "No matching entry in directory history"
298 return
298 return
299 else:
299 else:
300 opts = {}
300 opts = {}
301
301
302
302
303 else:
303 else:
304 #turn all non-space-escaping backslashes to slashes,
304 #turn all non-space-escaping backslashes to slashes,
305 # for c:\windows\directory\names\
305 # for c:\windows\directory\names\
306 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
306 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
307 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
307 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
308 # jump to previous
308 # jump to previous
309 if ps == '-':
309 if ps == '-':
310 try:
310 try:
311 ps = self.shell.user_ns['_dh'][-2]
311 ps = self.shell.user_ns['_dh'][-2]
312 except IndexError:
312 except IndexError:
313 raise UsageError('%cd -: No previous directory to change to.')
313 raise UsageError('%cd -: No previous directory to change to.')
314 # jump to bookmark if needed
314 # jump to bookmark if needed
315 else:
315 else:
316 if not os.path.isdir(ps) or opts.has_key('b'):
316 if not os.path.isdir(ps) or opts.has_key('b'):
317 bkms = self.shell.db.get('bookmarks', {})
317 bkms = self.shell.db.get('bookmarks', {})
318
318
319 if bkms.has_key(ps):
319 if bkms.has_key(ps):
320 target = bkms[ps]
320 target = bkms[ps]
321 print '(bookmark:%s) -> %s' % (ps,target)
321 print '(bookmark:%s) -> %s' % (ps,target)
322 ps = target
322 ps = target
323 else:
323 else:
324 if opts.has_key('b'):
324 if opts.has_key('b'):
325 raise UsageError("Bookmark '%s' not found. "
325 raise UsageError("Bookmark '%s' not found. "
326 "Use '%%bookmark -l' to see your bookmarks." % ps)
326 "Use '%%bookmark -l' to see your bookmarks." % ps)
327
327
328 # strip extra quotes on Windows, because os.chdir doesn't like them
328 # strip extra quotes on Windows, because os.chdir doesn't like them
329 ps = unquote_filename(ps)
329 ps = unquote_filename(ps)
330 # at this point ps should point to the target dir
330 # at this point ps should point to the target dir
331 if ps:
331 if ps:
332 try:
332 try:
333 os.chdir(os.path.expanduser(ps))
333 os.chdir(os.path.expanduser(ps))
334 if hasattr(self.shell, 'term_title') and self.shell.term_title:
334 if hasattr(self.shell, 'term_title') and self.shell.term_title:
335 set_term_title('IPython: ' + abbrev_cwd())
335 set_term_title('IPython: ' + abbrev_cwd())
336 except OSError:
336 except OSError:
337 print sys.exc_info()[1]
337 print sys.exc_info()[1]
338 else:
338 else:
339 cwd = os.getcwdu()
339 cwd = os.getcwdu()
340 dhist = self.shell.user_ns['_dh']
340 dhist = self.shell.user_ns['_dh']
341 if oldcwd != cwd:
341 if oldcwd != cwd:
342 dhist.append(cwd)
342 dhist.append(cwd)
343 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
343 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
344
344
345 else:
345 else:
346 os.chdir(self.shell.home_dir)
346 os.chdir(self.shell.home_dir)
347 if hasattr(self.shell, 'term_title') and self.shell.term_title:
347 if hasattr(self.shell, 'term_title') and self.shell.term_title:
348 set_term_title('IPython: ' + '~')
348 set_term_title('IPython: ' + '~')
349 cwd = os.getcwdu()
349 cwd = os.getcwdu()
350 dhist = self.shell.user_ns['_dh']
350 dhist = self.shell.user_ns['_dh']
351
351
352 if oldcwd != cwd:
352 if oldcwd != cwd:
353 dhist.append(cwd)
353 dhist.append(cwd)
354 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
354 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
355 if not 'q' in opts and self.shell.user_ns['_dh']:
355 if not 'q' in opts and self.shell.user_ns['_dh']:
356 print self.shell.user_ns['_dh'][-1]
356 print self.shell.user_ns['_dh'][-1]
357
357
358
358
359 @line_magic
359 @line_magic
360 def env(self, parameter_s=''):
360 def env(self, parameter_s=''):
361 """List environment variables."""
361 """List environment variables."""
362
362
363 return dict(os.environ)
363 return dict(os.environ)
364
364
365 @line_magic
365 @line_magic
366 def pushd(self, parameter_s=''):
366 def pushd(self, parameter_s=''):
367 """Place the current dir on stack and change directory.
367 """Place the current dir on stack and change directory.
368
368
369 Usage:\\
369 Usage:\\
370 %pushd ['dirname']
370 %pushd ['dirname']
371 """
371 """
372
372
373 dir_s = self.shell.dir_stack
373 dir_s = self.shell.dir_stack
374 tgt = os.path.expanduser(unquote_filename(parameter_s))
374 tgt = os.path.expanduser(unquote_filename(parameter_s))
375 cwd = os.getcwdu().replace(self.shell.home_dir,'~')
375 cwd = os.getcwdu().replace(self.shell.home_dir,'~')
376 if tgt:
376 if tgt:
377 self.cd(parameter_s)
377 self.cd(parameter_s)
378 dir_s.insert(0,cwd)
378 dir_s.insert(0,cwd)
379 return self.shell.magic('dirs')
379 return self.shell.magic('dirs')
380
380
381 @line_magic
381 @line_magic
382 def popd(self, parameter_s=''):
382 def popd(self, parameter_s=''):
383 """Change to directory popped off the top of the stack.
383 """Change to directory popped off the top of the stack.
384 """
384 """
385 if not self.shell.dir_stack:
385 if not self.shell.dir_stack:
386 raise UsageError("%popd on empty stack")
386 raise UsageError("%popd on empty stack")
387 top = self.shell.dir_stack.pop(0)
387 top = self.shell.dir_stack.pop(0)
388 self.cd(top)
388 self.cd(top)
389 print "popd ->",top
389 print "popd ->",top
390
390
391 @line_magic
391 @line_magic
392 def dirs(self, parameter_s=''):
392 def dirs(self, parameter_s=''):
393 """Return the current directory stack."""
393 """Return the current directory stack."""
394
394
395 return self.shell.dir_stack
395 return self.shell.dir_stack
396
396
397 @line_magic
397 @line_magic
398 def dhist(self, parameter_s=''):
398 def dhist(self, parameter_s=''):
399 """Print your history of visited directories.
399 """Print your history of visited directories.
400
400
401 %dhist -> print full history\\
401 %dhist -> print full history\\
402 %dhist n -> print last n entries only\\
402 %dhist n -> print last n entries only\\
403 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
403 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
404
404
405 This history is automatically maintained by the %cd command, and
405 This history is automatically maintained by the %cd command, and
406 always available as the global list variable _dh. You can use %cd -<n>
406 always available as the global list variable _dh. You can use %cd -<n>
407 to go to directory number <n>.
407 to go to directory number <n>.
408
408
409 Note that most of time, you should view directory history by entering
409 Note that most of time, you should view directory history by entering
410 cd -<TAB>.
410 cd -<TAB>.
411
411
412 """
412 """
413
413
414 dh = self.shell.user_ns['_dh']
414 dh = self.shell.user_ns['_dh']
415 if parameter_s:
415 if parameter_s:
416 try:
416 try:
417 args = map(int,parameter_s.split())
417 args = map(int,parameter_s.split())
418 except:
418 except:
419 self.arg_err(self.dhist)
419 self.arg_err(self.dhist)
420 return
420 return
421 if len(args) == 1:
421 if len(args) == 1:
422 ini,fin = max(len(dh)-(args[0]),0),len(dh)
422 ini,fin = max(len(dh)-(args[0]),0),len(dh)
423 elif len(args) == 2:
423 elif len(args) == 2:
424 ini,fin = args
424 ini,fin = args
425 else:
425 else:
426 self.arg_err(self.dhist)
426 self.arg_err(self.dhist)
427 return
427 return
428 else:
428 else:
429 ini,fin = 0,len(dh)
429 ini,fin = 0,len(dh)
430 nlprint(dh,
430 nlprint(dh,
431 header = 'Directory history (kept in _dh)',
431 header = 'Directory history (kept in _dh)',
432 start=ini,stop=fin)
432 start=ini,stop=fin)
433
433
434 @skip_doctest
434 @skip_doctest
435 @line_magic
435 @line_magic
436 def sc(self, parameter_s=''):
436 def sc(self, parameter_s=''):
437 """Shell capture - execute a shell command and capture its output.
437 """Shell capture - execute a shell command and capture its output.
438
438
439 DEPRECATED. Suboptimal, retained for backwards compatibility.
439 DEPRECATED. Suboptimal, retained for backwards compatibility.
440
440
441 You should use the form 'var = !command' instead. Example:
441 You should use the form 'var = !command' instead. Example:
442
442
443 "%sc -l myfiles = ls ~" should now be written as
443 "%sc -l myfiles = ls ~" should now be written as
444
444
445 "myfiles = !ls ~"
445 "myfiles = !ls ~"
446
446
447 myfiles.s, myfiles.l and myfiles.n still apply as documented
447 myfiles.s, myfiles.l and myfiles.n still apply as documented
448 below.
448 below.
449
449
450 --
450 --
451 %sc [options] varname=command
451 %sc [options] varname=command
452
452
453 IPython will run the given command using commands.getoutput(), and
453 IPython will run the given command using commands.getoutput(), and
454 will then update the user's interactive namespace with a variable
454 will then update the user's interactive namespace with a variable
455 called varname, containing the value of the call. Your command can
455 called varname, containing the value of the call. Your command can
456 contain shell wildcards, pipes, etc.
456 contain shell wildcards, pipes, etc.
457
457
458 The '=' sign in the syntax is mandatory, and the variable name you
458 The '=' sign in the syntax is mandatory, and the variable name you
459 supply must follow Python's standard conventions for valid names.
459 supply must follow Python's standard conventions for valid names.
460
460
461 (A special format without variable name exists for internal use)
461 (A special format without variable name exists for internal use)
462
462
463 Options:
463 Options:
464
464
465 -l: list output. Split the output on newlines into a list before
465 -l: list output. Split the output on newlines into a list before
466 assigning it to the given variable. By default the output is stored
466 assigning it to the given variable. By default the output is stored
467 as a single string.
467 as a single string.
468
468
469 -v: verbose. Print the contents of the variable.
469 -v: verbose. Print the contents of the variable.
470
470
471 In most cases you should not need to split as a list, because the
471 In most cases you should not need to split as a list, because the
472 returned value is a special type of string which can automatically
472 returned value is a special type of string which can automatically
473 provide its contents either as a list (split on newlines) or as a
473 provide its contents either as a list (split on newlines) or as a
474 space-separated string. These are convenient, respectively, either
474 space-separated string. These are convenient, respectively, either
475 for sequential processing or to be passed to a shell command.
475 for sequential processing or to be passed to a shell command.
476
476
477 For example::
477 For example::
478
478
479 # Capture into variable a
479 # Capture into variable a
480 In [1]: sc a=ls *py
480 In [1]: sc a=ls *py
481
481
482 # a is a string with embedded newlines
482 # a is a string with embedded newlines
483 In [2]: a
483 In [2]: a
484 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
484 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
485
485
486 # which can be seen as a list:
486 # which can be seen as a list:
487 In [3]: a.l
487 In [3]: a.l
488 Out[3]: ['setup.py', 'win32_manual_post_install.py']
488 Out[3]: ['setup.py', 'win32_manual_post_install.py']
489
489
490 # or as a whitespace-separated string:
490 # or as a whitespace-separated string:
491 In [4]: a.s
491 In [4]: a.s
492 Out[4]: 'setup.py win32_manual_post_install.py'
492 Out[4]: 'setup.py win32_manual_post_install.py'
493
493
494 # a.s is useful to pass as a single command line:
494 # a.s is useful to pass as a single command line:
495 In [5]: !wc -l $a.s
495 In [5]: !wc -l $a.s
496 146 setup.py
496 146 setup.py
497 130 win32_manual_post_install.py
497 130 win32_manual_post_install.py
498 276 total
498 276 total
499
499
500 # while the list form is useful to loop over:
500 # while the list form is useful to loop over:
501 In [6]: for f in a.l:
501 In [6]: for f in a.l:
502 ...: !wc -l $f
502 ...: !wc -l $f
503 ...:
503 ...:
504 146 setup.py
504 146 setup.py
505 130 win32_manual_post_install.py
505 130 win32_manual_post_install.py
506
506
507 Similarly, the lists returned by the -l option are also special, in
507 Similarly, the lists returned by the -l option are also special, in
508 the sense that you can equally invoke the .s attribute on them to
508 the sense that you can equally invoke the .s attribute on them to
509 automatically get a whitespace-separated string from their contents::
509 automatically get a whitespace-separated string from their contents::
510
510
511 In [7]: sc -l b=ls *py
511 In [7]: sc -l b=ls *py
512
512
513 In [8]: b
513 In [8]: b
514 Out[8]: ['setup.py', 'win32_manual_post_install.py']
514 Out[8]: ['setup.py', 'win32_manual_post_install.py']
515
515
516 In [9]: b.s
516 In [9]: b.s
517 Out[9]: 'setup.py win32_manual_post_install.py'
517 Out[9]: 'setup.py win32_manual_post_install.py'
518
518
519 In summary, both the lists and strings used for output capture have
519 In summary, both the lists and strings used for output capture have
520 the following special attributes::
520 the following special attributes::
521
521
522 .l (or .list) : value as list.
522 .l (or .list) : value as list.
523 .n (or .nlstr): value as newline-separated string.
523 .n (or .nlstr): value as newline-separated string.
524 .s (or .spstr): value as space-separated string.
524 .s (or .spstr): value as space-separated string.
525 """
525 """
526
526
527 opts,args = self.parse_options(parameter_s,'lv')
527 opts,args = self.parse_options(parameter_s,'lv')
528 # Try to get a variable name and command to run
528 # Try to get a variable name and command to run
529 try:
529 try:
530 # the variable name must be obtained from the parse_options
530 # the variable name must be obtained from the parse_options
531 # output, which uses shlex.split to strip options out.
531 # output, which uses shlex.split to strip options out.
532 var,_ = args.split('=',1)
532 var,_ = args.split('=',1)
533 var = var.strip()
533 var = var.strip()
534 # But the command has to be extracted from the original input
534 # But the command has to be extracted from the original input
535 # parameter_s, not on what parse_options returns, to avoid the
535 # parameter_s, not on what parse_options returns, to avoid the
536 # quote stripping which shlex.split performs on it.
536 # quote stripping which shlex.split performs on it.
537 _,cmd = parameter_s.split('=',1)
537 _,cmd = parameter_s.split('=',1)
538 except ValueError:
538 except ValueError:
539 var,cmd = '',''
539 var,cmd = '',''
540 # If all looks ok, proceed
540 # If all looks ok, proceed
541 split = 'l' in opts
541 split = 'l' in opts
542 out = self.shell.getoutput(cmd, split=split)
542 out = self.shell.getoutput(cmd, split=split)
543 if opts.has_key('v'):
543 if opts.has_key('v'):
544 print '%s ==\n%s' % (var,pformat(out))
544 print '%s ==\n%s' % (var,pformat(out))
545 if var:
545 if var:
546 self.shell.user_ns.update({var:out})
546 self.shell.user_ns.update({var:out})
547 else:
547 else:
548 return out
548 return out
549
549
550 @line_magic
550 @line_magic
551 def sx(self, parameter_s=''):
551 def sx(self, parameter_s=''):
552 """Shell execute - run a shell command and capture its output.
552 """Shell execute - run a shell command and capture its output.
553
553
554 %sx command
554 %sx command
555
555
556 IPython will run the given command using commands.getoutput(), and
556 IPython will run the given command using commands.getoutput(), and
557 return the result formatted as a list (split on '\\n'). Since the
557 return the result formatted as a list (split on '\\n'). Since the
558 output is _returned_, it will be stored in ipython's regular output
558 output is _returned_, it will be stored in ipython's regular output
559 cache Out[N] and in the '_N' automatic variables.
559 cache Out[N] and in the '_N' automatic variables.
560
560
561 Notes:
561 Notes:
562
562
563 1) If an input line begins with '!!', then %sx is automatically
563 1) If an input line begins with '!!', then %sx is automatically
564 invoked. That is, while::
564 invoked. That is, while::
565
565
566 !ls
566 !ls
567
567
568 causes ipython to simply issue system('ls'), typing::
568 causes ipython to simply issue system('ls'), typing::
569
569
570 !!ls
570 !!ls
571
571
572 is a shorthand equivalent to::
572 is a shorthand equivalent to::
573
573
574 %sx ls
574 %sx ls
575
575
576 2) %sx differs from %sc in that %sx automatically splits into a list,
576 2) %sx differs from %sc in that %sx automatically splits into a list,
577 like '%sc -l'. The reason for this is to make it as easy as possible
577 like '%sc -l'. The reason for this is to make it as easy as possible
578 to process line-oriented shell output via further python commands.
578 to process line-oriented shell output via further python commands.
579 %sc is meant to provide much finer control, but requires more
579 %sc is meant to provide much finer control, but requires more
580 typing.
580 typing.
581
581
582 3) Just like %sc -l, this is a list with special attributes:
582 3) Just like %sc -l, this is a list with special attributes:
583 ::
583 ::
584
584
585 .l (or .list) : value as list.
585 .l (or .list) : value as list.
586 .n (or .nlstr): value as newline-separated string.
586 .n (or .nlstr): value as newline-separated string.
587 .s (or .spstr): value as whitespace-separated string.
587 .s (or .spstr): value as whitespace-separated string.
588
588
589 This is very useful when trying to use such lists as arguments to
589 This is very useful when trying to use such lists as arguments to
590 system commands."""
590 system commands."""
591
591
592 if parameter_s:
592 if parameter_s:
593 return self.shell.getoutput(parameter_s)
593 return self.shell.getoutput(parameter_s)
594
594
595
595
596 @line_magic
596 @line_magic
597 def bookmark(self, parameter_s=''):
597 def bookmark(self, parameter_s=''):
598 """Manage IPython's bookmark system.
598 """Manage IPython's bookmark system.
599
599
600 %bookmark <name> - set bookmark to current dir
600 %bookmark <name> - set bookmark to current dir
601 %bookmark <name> <dir> - set bookmark to <dir>
601 %bookmark <name> <dir> - set bookmark to <dir>
602 %bookmark -l - list all bookmarks
602 %bookmark -l - list all bookmarks
603 %bookmark -d <name> - remove bookmark
603 %bookmark -d <name> - remove bookmark
604 %bookmark -r - remove all bookmarks
604 %bookmark -r - remove all bookmarks
605
605
606 You can later on access a bookmarked folder with::
606 You can later on access a bookmarked folder with::
607
607
608 %cd -b <name>
608 %cd -b <name>
609
609
610 or simply '%cd <name>' if there is no directory called <name> AND
610 or simply '%cd <name>' if there is no directory called <name> AND
611 there is such a bookmark defined.
611 there is such a bookmark defined.
612
612
613 Your bookmarks persist through IPython sessions, but they are
613 Your bookmarks persist through IPython sessions, but they are
614 associated with each profile."""
614 associated with each profile."""
615
615
616 opts,args = self.parse_options(parameter_s,'drl',mode='list')
616 opts,args = self.parse_options(parameter_s,'drl',mode='list')
617 if len(args) > 2:
617 if len(args) > 2:
618 raise UsageError("%bookmark: too many arguments")
618 raise UsageError("%bookmark: too many arguments")
619
619
620 bkms = self.shell.db.get('bookmarks',{})
620 bkms = self.shell.db.get('bookmarks',{})
621
621
622 if opts.has_key('d'):
622 if opts.has_key('d'):
623 try:
623 try:
624 todel = args[0]
624 todel = args[0]
625 except IndexError:
625 except IndexError:
626 raise UsageError(
626 raise UsageError(
627 "%bookmark -d: must provide a bookmark to delete")
627 "%bookmark -d: must provide a bookmark to delete")
628 else:
628 else:
629 try:
629 try:
630 del bkms[todel]
630 del bkms[todel]
631 except KeyError:
631 except KeyError:
632 raise UsageError(
632 raise UsageError(
633 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
633 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
634
634
635 elif opts.has_key('r'):
635 elif opts.has_key('r'):
636 bkms = {}
636 bkms = {}
637 elif opts.has_key('l'):
637 elif opts.has_key('l'):
638 bks = bkms.keys()
638 bks = bkms.keys()
639 bks.sort()
639 bks.sort()
640 if bks:
640 if bks:
641 size = max(map(len,bks))
641 size = max(map(len,bks))
642 else:
642 else:
643 size = 0
643 size = 0
644 fmt = '%-'+str(size)+'s -> %s'
644 fmt = '%-'+str(size)+'s -> %s'
645 print 'Current bookmarks:'
645 print 'Current bookmarks:'
646 for bk in bks:
646 for bk in bks:
647 print fmt % (bk,bkms[bk])
647 print fmt % (bk,bkms[bk])
648 else:
648 else:
649 if not args:
649 if not args:
650 raise UsageError("%bookmark: You must specify the bookmark name")
650 raise UsageError("%bookmark: You must specify the bookmark name")
651 elif len(args)==1:
651 elif len(args)==1:
652 bkms[args[0]] = os.getcwdu()
652 bkms[args[0]] = os.getcwdu()
653 elif len(args)==2:
653 elif len(args)==2:
654 bkms[args[0]] = args[1]
654 bkms[args[0]] = args[1]
655 self.shell.db['bookmarks'] = bkms
655 self.shell.db['bookmarks'] = bkms
656
656
657 @line_magic
657 @line_magic
658 def pycat(self, parameter_s=''):
658 def pycat(self, parameter_s=''):
659 """Show a syntax-highlighted file through a pager.
659 """Show a syntax-highlighted file through a pager.
660
660
661 This magic is similar to the cat utility, but it will assume the file
661 This magic is similar to the cat utility, but it will assume the file
662 to be Python source and will show it with syntax highlighting. """
662 to be Python source and will show it with syntax highlighting. """
663
663
664 try:
664 try:
665 filename = get_py_filename(parameter_s)
665 filename = get_py_filename(parameter_s)
666 cont = file_read(filename)
666 cont = file_read(filename)
667 except IOError:
667 except IOError:
668 try:
668 try:
669 cont = eval(parameter_s, self.shell.user_ns)
669 cont = eval(parameter_s, self.shell.user_ns)
670 except NameError:
670 except NameError:
671 cont = None
671 cont = None
672 if cont is None:
672 if cont is None:
673 print "Error: no such file or variable"
673 print "Error: no such file or variable"
674 return
674 return
675
675
676 page.page(self.shell.pycolorize(cont))
676 page.page(self.shell.pycolorize(cont))
@@ -1,88 +1,88 b''
1 """Implementation of magic functions for matplotlib/pylab support.
1 """Implementation of magic functions for matplotlib/pylab support.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2012 The IPython Development Team.
4 # Copyright (c) 2012 The IPython Development Team.
5 #
5 #
6 # Distributed under the terms of the Modified BSD License.
6 # Distributed under the terms of the Modified BSD License.
7 #
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
10
10
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 # Our own packages
15 # Our own packages
16 from IPython.config.application import Application
16 from IPython.config.application import Application
17 from IPython.core.magic import Magics, register_magics, line_magic
17 from IPython.core.magic import Magics, magics_class, line_magic
18 from IPython.testing.skipdoctest import skip_doctest
18 from IPython.testing.skipdoctest import skip_doctest
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Magic implementation classes
21 # Magic implementation classes
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23
23
24 @register_magics
24 @magics_class
25 class PylabMagics(Magics):
25 class PylabMagics(Magics):
26 """Magics related to matplotlib's pylab support"""
26 """Magics related to matplotlib's pylab support"""
27
27
28 @skip_doctest
28 @skip_doctest
29 @line_magic
29 @line_magic
30 def pylab(self, parameter_s=''):
30 def pylab(self, parameter_s=''):
31 """Load numpy and matplotlib to work interactively.
31 """Load numpy and matplotlib to work interactively.
32
32
33 %pylab [GUINAME]
33 %pylab [GUINAME]
34
34
35 This function lets you activate pylab (matplotlib, numpy and
35 This function lets you activate pylab (matplotlib, numpy and
36 interactive support) at any point during an IPython session.
36 interactive support) at any point during an IPython session.
37
37
38 It will import at the top level numpy as np, pyplot as plt, matplotlib,
38 It will import at the top level numpy as np, pyplot as plt, matplotlib,
39 pylab and mlab, as well as all names from numpy and pylab.
39 pylab and mlab, as well as all names from numpy and pylab.
40
40
41 If you are using the inline matplotlib backend for embedded figures,
41 If you are using the inline matplotlib backend for embedded figures,
42 you can adjust its behavior via the %config magic::
42 you can adjust its behavior via the %config magic::
43
43
44 # enable SVG figures, necessary for SVG+XHTML export in the qtconsole
44 # enable SVG figures, necessary for SVG+XHTML export in the qtconsole
45 In [1]: %config InlineBackend.figure_format = 'svg'
45 In [1]: %config InlineBackend.figure_format = 'svg'
46
46
47 # change the behavior of closing all figures at the end of each
47 # change the behavior of closing all figures at the end of each
48 # execution (cell), or allowing reuse of active figures across
48 # execution (cell), or allowing reuse of active figures across
49 # cells:
49 # cells:
50 In [2]: %config InlineBackend.close_figures = False
50 In [2]: %config InlineBackend.close_figures = False
51
51
52 Parameters
52 Parameters
53 ----------
53 ----------
54 guiname : optional
54 guiname : optional
55 One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk',
55 One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk',
56 'osx' or 'tk'). If given, the corresponding Matplotlib backend is
56 'osx' or 'tk'). If given, the corresponding Matplotlib backend is
57 used, otherwise matplotlib's default (which you can override in your
57 used, otherwise matplotlib's default (which you can override in your
58 matplotlib config file) is used.
58 matplotlib config file) is used.
59
59
60 Examples
60 Examples
61 --------
61 --------
62 In this case, where the MPL default is TkAgg::
62 In this case, where the MPL default is TkAgg::
63
63
64 In [2]: %pylab
64 In [2]: %pylab
65
65
66 Welcome to pylab, a matplotlib-based Python environment.
66 Welcome to pylab, a matplotlib-based Python environment.
67 Backend in use: TkAgg
67 Backend in use: TkAgg
68 For more information, type 'help(pylab)'.
68 For more information, type 'help(pylab)'.
69
69
70 But you can explicitly request a different backend::
70 But you can explicitly request a different backend::
71
71
72 In [3]: %pylab qt
72 In [3]: %pylab qt
73
73
74 Welcome to pylab, a matplotlib-based Python environment.
74 Welcome to pylab, a matplotlib-based Python environment.
75 Backend in use: Qt4Agg
75 Backend in use: Qt4Agg
76 For more information, type 'help(pylab)'.
76 For more information, type 'help(pylab)'.
77 """
77 """
78
78
79 if Application.initialized():
79 if Application.initialized():
80 app = Application.instance()
80 app = Application.instance()
81 try:
81 try:
82 import_all_status = app.pylab_import_all
82 import_all_status = app.pylab_import_all
83 except AttributeError:
83 except AttributeError:
84 import_all_status = True
84 import_all_status = True
85 else:
85 else:
86 import_all_status = True
86 import_all_status = True
87
87
88 self.shell.enable_pylab(parameter_s, import_all=import_all_status)
88 self.shell.enable_pylab(parameter_s, import_all=import_all_status)
@@ -1,487 +1,487 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tests for various magic functions.
2 """Tests for various magic functions.
3
3
4 Needs to be run by nose (to make ipython session available).
4 Needs to be run by nose (to make ipython session available).
5 """
5 """
6 from __future__ import absolute_import
6 from __future__ import absolute_import
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Imports
9 # Imports
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
11
12 import io
12 import io
13 import os
13 import os
14 import sys
14 import sys
15 from StringIO import StringIO
15 from StringIO import StringIO
16
16
17 import nose.tools as nt
17 import nose.tools as nt
18
18
19 from IPython.core import magic
19 from IPython.core import magic
20 from IPython.core.magics import execution
20 from IPython.core.magics import execution
21 from IPython.nbformat.v3.tests.nbexamples import nb0
21 from IPython.nbformat.v3.tests.nbexamples import nb0
22 from IPython.nbformat import current
22 from IPython.nbformat import current
23 from IPython.testing import decorators as dec
23 from IPython.testing import decorators as dec
24 from IPython.testing import tools as tt
24 from IPython.testing import tools as tt
25 from IPython.utils import py3compat
25 from IPython.utils import py3compat
26 from IPython.utils.tempdir import TemporaryDirectory
26 from IPython.utils.tempdir import TemporaryDirectory
27
27
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 # Test functions begin
29 # Test functions begin
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31
31
32 @magic.register_magics
32 @magic.magics_class
33 class DummyMagics(magic.Magics): pass
33 class DummyMagics(magic.Magics): pass
34
34
35 def test_rehashx():
35 def test_rehashx():
36 # clear up everything
36 # clear up everything
37 _ip = get_ipython()
37 _ip = get_ipython()
38 _ip.alias_manager.alias_table.clear()
38 _ip.alias_manager.alias_table.clear()
39 del _ip.db['syscmdlist']
39 del _ip.db['syscmdlist']
40
40
41 _ip.magic('rehashx')
41 _ip.magic('rehashx')
42 # Practically ALL ipython development systems will have more than 10 aliases
42 # Practically ALL ipython development systems will have more than 10 aliases
43
43
44 yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10)
44 yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10)
45 for key, val in _ip.alias_manager.alias_table.iteritems():
45 for key, val in _ip.alias_manager.alias_table.iteritems():
46 # we must strip dots from alias names
46 # we must strip dots from alias names
47 nt.assert_true('.' not in key)
47 nt.assert_true('.' not in key)
48
48
49 # rehashx must fill up syscmdlist
49 # rehashx must fill up syscmdlist
50 scoms = _ip.db['syscmdlist']
50 scoms = _ip.db['syscmdlist']
51 yield (nt.assert_true, len(scoms) > 10)
51 yield (nt.assert_true, len(scoms) > 10)
52
52
53
53
54 def test_magic_parse_options():
54 def test_magic_parse_options():
55 """Test that we don't mangle paths when parsing magic options."""
55 """Test that we don't mangle paths when parsing magic options."""
56 ip = get_ipython()
56 ip = get_ipython()
57 path = 'c:\\x'
57 path = 'c:\\x'
58 m = DummyMagics(ip)
58 m = DummyMagics(ip)
59 opts = m.parse_options('-f %s' % path,'f:')[0]
59 opts = m.parse_options('-f %s' % path,'f:')[0]
60 # argv splitting is os-dependent
60 # argv splitting is os-dependent
61 if os.name == 'posix':
61 if os.name == 'posix':
62 expected = 'c:x'
62 expected = 'c:x'
63 else:
63 else:
64 expected = path
64 expected = path
65 nt.assert_equals(opts['f'], expected)
65 nt.assert_equals(opts['f'], expected)
66
66
67
67
68 @dec.skip_without('sqlite3')
68 @dec.skip_without('sqlite3')
69 def doctest_hist_f():
69 def doctest_hist_f():
70 """Test %hist -f with temporary filename.
70 """Test %hist -f with temporary filename.
71
71
72 In [9]: import tempfile
72 In [9]: import tempfile
73
73
74 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
74 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
75
75
76 In [11]: %hist -nl -f $tfile 3
76 In [11]: %hist -nl -f $tfile 3
77
77
78 In [13]: import os; os.unlink(tfile)
78 In [13]: import os; os.unlink(tfile)
79 """
79 """
80
80
81
81
82 @dec.skip_without('sqlite3')
82 @dec.skip_without('sqlite3')
83 def doctest_hist_r():
83 def doctest_hist_r():
84 """Test %hist -r
84 """Test %hist -r
85
85
86 XXX - This test is not recording the output correctly. For some reason, in
86 XXX - This test is not recording the output correctly. For some reason, in
87 testing mode the raw history isn't getting populated. No idea why.
87 testing mode the raw history isn't getting populated. No idea why.
88 Disabling the output checking for now, though at least we do run it.
88 Disabling the output checking for now, though at least we do run it.
89
89
90 In [1]: 'hist' in _ip.lsmagic()
90 In [1]: 'hist' in _ip.lsmagic()
91 Out[1]: True
91 Out[1]: True
92
92
93 In [2]: x=1
93 In [2]: x=1
94
94
95 In [3]: %hist -rl 2
95 In [3]: %hist -rl 2
96 x=1 # random
96 x=1 # random
97 %hist -r 2
97 %hist -r 2
98 """
98 """
99
99
100
100
101 @dec.skip_without('sqlite3')
101 @dec.skip_without('sqlite3')
102 def doctest_hist_op():
102 def doctest_hist_op():
103 """Test %hist -op
103 """Test %hist -op
104
104
105 In [1]: class b(float):
105 In [1]: class b(float):
106 ...: pass
106 ...: pass
107 ...:
107 ...:
108
108
109 In [2]: class s(object):
109 In [2]: class s(object):
110 ...: def __str__(self):
110 ...: def __str__(self):
111 ...: return 's'
111 ...: return 's'
112 ...:
112 ...:
113
113
114 In [3]:
114 In [3]:
115
115
116 In [4]: class r(b):
116 In [4]: class r(b):
117 ...: def __repr__(self):
117 ...: def __repr__(self):
118 ...: return 'r'
118 ...: return 'r'
119 ...:
119 ...:
120
120
121 In [5]: class sr(s,r): pass
121 In [5]: class sr(s,r): pass
122 ...:
122 ...:
123
123
124 In [6]:
124 In [6]:
125
125
126 In [7]: bb=b()
126 In [7]: bb=b()
127
127
128 In [8]: ss=s()
128 In [8]: ss=s()
129
129
130 In [9]: rr=r()
130 In [9]: rr=r()
131
131
132 In [10]: ssrr=sr()
132 In [10]: ssrr=sr()
133
133
134 In [11]: 4.5
134 In [11]: 4.5
135 Out[11]: 4.5
135 Out[11]: 4.5
136
136
137 In [12]: str(ss)
137 In [12]: str(ss)
138 Out[12]: 's'
138 Out[12]: 's'
139
139
140 In [13]:
140 In [13]:
141
141
142 In [14]: %hist -op
142 In [14]: %hist -op
143 >>> class b:
143 >>> class b:
144 ... pass
144 ... pass
145 ...
145 ...
146 >>> class s(b):
146 >>> class s(b):
147 ... def __str__(self):
147 ... def __str__(self):
148 ... return 's'
148 ... return 's'
149 ...
149 ...
150 >>>
150 >>>
151 >>> class r(b):
151 >>> class r(b):
152 ... def __repr__(self):
152 ... def __repr__(self):
153 ... return 'r'
153 ... return 'r'
154 ...
154 ...
155 >>> class sr(s,r): pass
155 >>> class sr(s,r): pass
156 >>>
156 >>>
157 >>> bb=b()
157 >>> bb=b()
158 >>> ss=s()
158 >>> ss=s()
159 >>> rr=r()
159 >>> rr=r()
160 >>> ssrr=sr()
160 >>> ssrr=sr()
161 >>> 4.5
161 >>> 4.5
162 4.5
162 4.5
163 >>> str(ss)
163 >>> str(ss)
164 's'
164 's'
165 >>>
165 >>>
166 """
166 """
167
167
168
168
169 @dec.skip_without('sqlite3')
169 @dec.skip_without('sqlite3')
170 def test_macro():
170 def test_macro():
171 ip = get_ipython()
171 ip = get_ipython()
172 ip.history_manager.reset() # Clear any existing history.
172 ip.history_manager.reset() # Clear any existing history.
173 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
173 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
174 for i, cmd in enumerate(cmds, start=1):
174 for i, cmd in enumerate(cmds, start=1):
175 ip.history_manager.store_inputs(i, cmd)
175 ip.history_manager.store_inputs(i, cmd)
176 ip.magic("macro test 1-3")
176 ip.magic("macro test 1-3")
177 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
177 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
178
178
179 # List macros.
179 # List macros.
180 assert "test" in ip.magic("macro")
180 assert "test" in ip.magic("macro")
181
181
182
182
183 @dec.skip_without('sqlite3')
183 @dec.skip_without('sqlite3')
184 def test_macro_run():
184 def test_macro_run():
185 """Test that we can run a multi-line macro successfully."""
185 """Test that we can run a multi-line macro successfully."""
186 ip = get_ipython()
186 ip = get_ipython()
187 ip.history_manager.reset()
187 ip.history_manager.reset()
188 cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"),
188 cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"),
189 "%macro test 2-3"]
189 "%macro test 2-3"]
190 for cmd in cmds:
190 for cmd in cmds:
191 ip.run_cell(cmd, store_history=True)
191 ip.run_cell(cmd, store_history=True)
192 nt.assert_equal(ip.user_ns["test"].value,
192 nt.assert_equal(ip.user_ns["test"].value,
193 py3compat.doctest_refactor_print("a+=1\nprint a\n"))
193 py3compat.doctest_refactor_print("a+=1\nprint a\n"))
194 with tt.AssertPrints("12"):
194 with tt.AssertPrints("12"):
195 ip.run_cell("test")
195 ip.run_cell("test")
196 with tt.AssertPrints("13"):
196 with tt.AssertPrints("13"):
197 ip.run_cell("test")
197 ip.run_cell("test")
198
198
199
199
200 @dec.skipif_not_numpy
200 @dec.skipif_not_numpy
201 def test_numpy_reset_array_undec():
201 def test_numpy_reset_array_undec():
202 "Test '%reset array' functionality"
202 "Test '%reset array' functionality"
203 _ip.ex('import numpy as np')
203 _ip.ex('import numpy as np')
204 _ip.ex('a = np.empty(2)')
204 _ip.ex('a = np.empty(2)')
205 yield (nt.assert_true, 'a' in _ip.user_ns)
205 yield (nt.assert_true, 'a' in _ip.user_ns)
206 _ip.magic('reset -f array')
206 _ip.magic('reset -f array')
207 yield (nt.assert_false, 'a' in _ip.user_ns)
207 yield (nt.assert_false, 'a' in _ip.user_ns)
208
208
209 def test_reset_out():
209 def test_reset_out():
210 "Test '%reset out' magic"
210 "Test '%reset out' magic"
211 _ip.run_cell("parrot = 'dead'", store_history=True)
211 _ip.run_cell("parrot = 'dead'", store_history=True)
212 # test '%reset -f out', make an Out prompt
212 # test '%reset -f out', make an Out prompt
213 _ip.run_cell("parrot", store_history=True)
213 _ip.run_cell("parrot", store_history=True)
214 nt.assert_true('dead' in [_ip.user_ns[x] for x in '_','__','___'])
214 nt.assert_true('dead' in [_ip.user_ns[x] for x in '_','__','___'])
215 _ip.magic('reset -f out')
215 _ip.magic('reset -f out')
216 nt.assert_false('dead' in [_ip.user_ns[x] for x in '_','__','___'])
216 nt.assert_false('dead' in [_ip.user_ns[x] for x in '_','__','___'])
217 nt.assert_true(len(_ip.user_ns['Out']) == 0)
217 nt.assert_true(len(_ip.user_ns['Out']) == 0)
218
218
219 def test_reset_in():
219 def test_reset_in():
220 "Test '%reset in' magic"
220 "Test '%reset in' magic"
221 # test '%reset -f in'
221 # test '%reset -f in'
222 _ip.run_cell("parrot", store_history=True)
222 _ip.run_cell("parrot", store_history=True)
223 nt.assert_true('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
223 nt.assert_true('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
224 _ip.magic('%reset -f in')
224 _ip.magic('%reset -f in')
225 nt.assert_false('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
225 nt.assert_false('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
226 nt.assert_true(len(set(_ip.user_ns['In'])) == 1)
226 nt.assert_true(len(set(_ip.user_ns['In'])) == 1)
227
227
228 def test_reset_dhist():
228 def test_reset_dhist():
229 "Test '%reset dhist' magic"
229 "Test '%reset dhist' magic"
230 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
230 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
231 _ip.magic('cd ' + os.path.dirname(nt.__file__))
231 _ip.magic('cd ' + os.path.dirname(nt.__file__))
232 _ip.magic('cd -')
232 _ip.magic('cd -')
233 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
233 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
234 _ip.magic('reset -f dhist')
234 _ip.magic('reset -f dhist')
235 nt.assert_true(len(_ip.user_ns['_dh']) == 0)
235 nt.assert_true(len(_ip.user_ns['_dh']) == 0)
236 _ip.run_cell("_dh = [d for d in tmp]") #restore
236 _ip.run_cell("_dh = [d for d in tmp]") #restore
237
237
238 def test_reset_in_length():
238 def test_reset_in_length():
239 "Test that '%reset in' preserves In[] length"
239 "Test that '%reset in' preserves In[] length"
240 _ip.run_cell("print 'foo'")
240 _ip.run_cell("print 'foo'")
241 _ip.run_cell("reset -f in")
241 _ip.run_cell("reset -f in")
242 nt.assert_true(len(_ip.user_ns['In']) == _ip.displayhook.prompt_count+1)
242 nt.assert_true(len(_ip.user_ns['In']) == _ip.displayhook.prompt_count+1)
243
243
244 def test_time():
244 def test_time():
245 _ip.magic('time None')
245 _ip.magic('time None')
246
246
247 def test_tb_syntaxerror():
247 def test_tb_syntaxerror():
248 """test %tb after a SyntaxError"""
248 """test %tb after a SyntaxError"""
249 ip = get_ipython()
249 ip = get_ipython()
250 ip.run_cell("for")
250 ip.run_cell("for")
251
251
252 # trap and validate stdout
252 # trap and validate stdout
253 save_stdout = sys.stdout
253 save_stdout = sys.stdout
254 try:
254 try:
255 sys.stdout = StringIO()
255 sys.stdout = StringIO()
256 ip.run_cell("%tb")
256 ip.run_cell("%tb")
257 out = sys.stdout.getvalue()
257 out = sys.stdout.getvalue()
258 finally:
258 finally:
259 sys.stdout = save_stdout
259 sys.stdout = save_stdout
260 # trim output, and only check the last line
260 # trim output, and only check the last line
261 last_line = out.rstrip().splitlines()[-1].strip()
261 last_line = out.rstrip().splitlines()[-1].strip()
262 nt.assert_equals(last_line, "SyntaxError: invalid syntax")
262 nt.assert_equals(last_line, "SyntaxError: invalid syntax")
263
263
264
264
265 @py3compat.doctest_refactor_print
265 @py3compat.doctest_refactor_print
266 def doctest_time():
266 def doctest_time():
267 """
267 """
268 In [10]: %time None
268 In [10]: %time None
269 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
269 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
270 Wall time: 0.00 s
270 Wall time: 0.00 s
271
271
272 In [11]: def f(kmjy):
272 In [11]: def f(kmjy):
273 ....: %time print 2*kmjy
273 ....: %time print 2*kmjy
274
274
275 In [12]: f(3)
275 In [12]: f(3)
276 6
276 6
277 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
277 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
278 Wall time: 0.00 s
278 Wall time: 0.00 s
279 """
279 """
280
280
281
281
282 def test_doctest_mode():
282 def test_doctest_mode():
283 "Toggle doctest_mode twice, it should be a no-op and run without error"
283 "Toggle doctest_mode twice, it should be a no-op and run without error"
284 _ip.magic('doctest_mode')
284 _ip.magic('doctest_mode')
285 _ip.magic('doctest_mode')
285 _ip.magic('doctest_mode')
286
286
287
287
288 def test_parse_options():
288 def test_parse_options():
289 """Tests for basic options parsing in magics."""
289 """Tests for basic options parsing in magics."""
290 # These are only the most minimal of tests, more should be added later. At
290 # These are only the most minimal of tests, more should be added later. At
291 # the very least we check that basic text/unicode calls work OK.
291 # the very least we check that basic text/unicode calls work OK.
292 m = DummyMagics(_ip)
292 m = DummyMagics(_ip)
293 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
293 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
294 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
294 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
295
295
296
296
297 def test_dirops():
297 def test_dirops():
298 """Test various directory handling operations."""
298 """Test various directory handling operations."""
299 # curpath = lambda :os.path.splitdrive(os.getcwdu())[1].replace('\\','/')
299 # curpath = lambda :os.path.splitdrive(os.getcwdu())[1].replace('\\','/')
300 curpath = os.getcwdu
300 curpath = os.getcwdu
301 startdir = os.getcwdu()
301 startdir = os.getcwdu()
302 ipdir = os.path.realpath(_ip.ipython_dir)
302 ipdir = os.path.realpath(_ip.ipython_dir)
303 try:
303 try:
304 _ip.magic('cd "%s"' % ipdir)
304 _ip.magic('cd "%s"' % ipdir)
305 nt.assert_equal(curpath(), ipdir)
305 nt.assert_equal(curpath(), ipdir)
306 _ip.magic('cd -')
306 _ip.magic('cd -')
307 nt.assert_equal(curpath(), startdir)
307 nt.assert_equal(curpath(), startdir)
308 _ip.magic('pushd "%s"' % ipdir)
308 _ip.magic('pushd "%s"' % ipdir)
309 nt.assert_equal(curpath(), ipdir)
309 nt.assert_equal(curpath(), ipdir)
310 _ip.magic('popd')
310 _ip.magic('popd')
311 nt.assert_equal(curpath(), startdir)
311 nt.assert_equal(curpath(), startdir)
312 finally:
312 finally:
313 os.chdir(startdir)
313 os.chdir(startdir)
314
314
315
315
316 def test_xmode():
316 def test_xmode():
317 # Calling xmode three times should be a no-op
317 # Calling xmode three times should be a no-op
318 xmode = _ip.InteractiveTB.mode
318 xmode = _ip.InteractiveTB.mode
319 for i in range(3):
319 for i in range(3):
320 _ip.magic("xmode")
320 _ip.magic("xmode")
321 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
321 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
322
322
323 def test_reset_hard():
323 def test_reset_hard():
324 monitor = []
324 monitor = []
325 class A(object):
325 class A(object):
326 def __del__(self):
326 def __del__(self):
327 monitor.append(1)
327 monitor.append(1)
328 def __repr__(self):
328 def __repr__(self):
329 return "<A instance>"
329 return "<A instance>"
330
330
331 _ip.user_ns["a"] = A()
331 _ip.user_ns["a"] = A()
332 _ip.run_cell("a")
332 _ip.run_cell("a")
333
333
334 nt.assert_equal(monitor, [])
334 nt.assert_equal(monitor, [])
335 _ip.magic("reset -f")
335 _ip.magic("reset -f")
336 nt.assert_equal(monitor, [1])
336 nt.assert_equal(monitor, [1])
337
337
338 class TestXdel(tt.TempFileMixin):
338 class TestXdel(tt.TempFileMixin):
339 def test_xdel(self):
339 def test_xdel(self):
340 """Test that references from %run are cleared by xdel."""
340 """Test that references from %run are cleared by xdel."""
341 src = ("class A(object):\n"
341 src = ("class A(object):\n"
342 " monitor = []\n"
342 " monitor = []\n"
343 " def __del__(self):\n"
343 " def __del__(self):\n"
344 " self.monitor.append(1)\n"
344 " self.monitor.append(1)\n"
345 "a = A()\n")
345 "a = A()\n")
346 self.mktmp(src)
346 self.mktmp(src)
347 # %run creates some hidden references...
347 # %run creates some hidden references...
348 _ip.magic("run %s" % self.fname)
348 _ip.magic("run %s" % self.fname)
349 # ... as does the displayhook.
349 # ... as does the displayhook.
350 _ip.run_cell("a")
350 _ip.run_cell("a")
351
351
352 monitor = _ip.user_ns["A"].monitor
352 monitor = _ip.user_ns["A"].monitor
353 nt.assert_equal(monitor, [])
353 nt.assert_equal(monitor, [])
354
354
355 _ip.magic("xdel a")
355 _ip.magic("xdel a")
356
356
357 # Check that a's __del__ method has been called.
357 # Check that a's __del__ method has been called.
358 nt.assert_equal(monitor, [1])
358 nt.assert_equal(monitor, [1])
359
359
360 def doctest_who():
360 def doctest_who():
361 """doctest for %who
361 """doctest for %who
362
362
363 In [1]: %reset -f
363 In [1]: %reset -f
364
364
365 In [2]: alpha = 123
365 In [2]: alpha = 123
366
366
367 In [3]: beta = 'beta'
367 In [3]: beta = 'beta'
368
368
369 In [4]: %who int
369 In [4]: %who int
370 alpha
370 alpha
371
371
372 In [5]: %who str
372 In [5]: %who str
373 beta
373 beta
374
374
375 In [6]: %whos
375 In [6]: %whos
376 Variable Type Data/Info
376 Variable Type Data/Info
377 ----------------------------
377 ----------------------------
378 alpha int 123
378 alpha int 123
379 beta str beta
379 beta str beta
380
380
381 In [7]: %who_ls
381 In [7]: %who_ls
382 Out[7]: ['alpha', 'beta']
382 Out[7]: ['alpha', 'beta']
383 """
383 """
384
384
385 def test_whos():
385 def test_whos():
386 """Check that whos is protected against objects where repr() fails."""
386 """Check that whos is protected against objects where repr() fails."""
387 class A(object):
387 class A(object):
388 def __repr__(self):
388 def __repr__(self):
389 raise Exception()
389 raise Exception()
390 _ip.user_ns['a'] = A()
390 _ip.user_ns['a'] = A()
391 _ip.magic("whos")
391 _ip.magic("whos")
392
392
393 @py3compat.u_format
393 @py3compat.u_format
394 def doctest_precision():
394 def doctest_precision():
395 """doctest for %precision
395 """doctest for %precision
396
396
397 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
397 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
398
398
399 In [2]: %precision 5
399 In [2]: %precision 5
400 Out[2]: {u}'%.5f'
400 Out[2]: {u}'%.5f'
401
401
402 In [3]: f.float_format
402 In [3]: f.float_format
403 Out[3]: {u}'%.5f'
403 Out[3]: {u}'%.5f'
404
404
405 In [4]: %precision %e
405 In [4]: %precision %e
406 Out[4]: {u}'%e'
406 Out[4]: {u}'%e'
407
407
408 In [5]: f(3.1415927)
408 In [5]: f(3.1415927)
409 Out[5]: {u}'3.141593e+00'
409 Out[5]: {u}'3.141593e+00'
410 """
410 """
411
411
412 def test_psearch():
412 def test_psearch():
413 with tt.AssertPrints("dict.fromkeys"):
413 with tt.AssertPrints("dict.fromkeys"):
414 _ip.run_cell("dict.fr*?")
414 _ip.run_cell("dict.fr*?")
415
415
416 def test_timeit_shlex():
416 def test_timeit_shlex():
417 """test shlex issues with timeit (#1109)"""
417 """test shlex issues with timeit (#1109)"""
418 _ip.ex("def f(*a,**kw): pass")
418 _ip.ex("def f(*a,**kw): pass")
419 _ip.magic('timeit -n1 "this is a bug".count(" ")')
419 _ip.magic('timeit -n1 "this is a bug".count(" ")')
420 _ip.magic('timeit -r1 -n1 f(" ", 1)')
420 _ip.magic('timeit -r1 -n1 f(" ", 1)')
421 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
421 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
422 _ip.magic('timeit -r1 -n1 ("a " + "b")')
422 _ip.magic('timeit -r1 -n1 ("a " + "b")')
423 _ip.magic('timeit -r1 -n1 f("a " + "b")')
423 _ip.magic('timeit -r1 -n1 f("a " + "b")')
424 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
424 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
425
425
426
426
427 def test_timeit_arguments():
427 def test_timeit_arguments():
428 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
428 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
429 _ip.magic("timeit ('#')")
429 _ip.magic("timeit ('#')")
430
430
431
431
432 @dec.skipif(execution.profile is None)
432 @dec.skipif(execution.profile is None)
433 def test_prun_quotes():
433 def test_prun_quotes():
434 "Test that prun does not clobber string escapes (GH #1302)"
434 "Test that prun does not clobber string escapes (GH #1302)"
435 _ip.magic("prun -q x = '\t'")
435 _ip.magic("prun -q x = '\t'")
436 nt.assert_equal(_ip.user_ns['x'], '\t')
436 nt.assert_equal(_ip.user_ns['x'], '\t')
437
437
438 def test_extension():
438 def test_extension():
439 tmpdir = TemporaryDirectory()
439 tmpdir = TemporaryDirectory()
440 orig_ipython_dir = _ip.ipython_dir
440 orig_ipython_dir = _ip.ipython_dir
441 try:
441 try:
442 _ip.ipython_dir = tmpdir.name
442 _ip.ipython_dir = tmpdir.name
443 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
443 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
444 url = os.path.join(os.path.dirname(__file__), "daft_extension.py")
444 url = os.path.join(os.path.dirname(__file__), "daft_extension.py")
445 _ip.magic("install_ext %s" % url)
445 _ip.magic("install_ext %s" % url)
446 _ip.user_ns.pop('arq', None)
446 _ip.user_ns.pop('arq', None)
447 _ip.magic("load_ext daft_extension")
447 _ip.magic("load_ext daft_extension")
448 tt.assert_equal(_ip.user_ns['arq'], 185)
448 tt.assert_equal(_ip.user_ns['arq'], 185)
449 _ip.magic("unload_ext daft_extension")
449 _ip.magic("unload_ext daft_extension")
450 assert 'arq' not in _ip.user_ns
450 assert 'arq' not in _ip.user_ns
451 finally:
451 finally:
452 _ip.ipython_dir = orig_ipython_dir
452 _ip.ipython_dir = orig_ipython_dir
453
453
454 def test_notebook_export_json():
454 def test_notebook_export_json():
455 with TemporaryDirectory() as td:
455 with TemporaryDirectory() as td:
456 outfile = os.path.join(td, "nb.ipynb")
456 outfile = os.path.join(td, "nb.ipynb")
457 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
457 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
458 _ip.magic("notebook -e %s" % outfile)
458 _ip.magic("notebook -e %s" % outfile)
459
459
460 def test_notebook_export_py():
460 def test_notebook_export_py():
461 with TemporaryDirectory() as td:
461 with TemporaryDirectory() as td:
462 outfile = os.path.join(td, "nb.py")
462 outfile = os.path.join(td, "nb.py")
463 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
463 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
464 _ip.magic("notebook -e %s" % outfile)
464 _ip.magic("notebook -e %s" % outfile)
465
465
466 def test_notebook_reformat_py():
466 def test_notebook_reformat_py():
467 with TemporaryDirectory() as td:
467 with TemporaryDirectory() as td:
468 infile = os.path.join(td, "nb.ipynb")
468 infile = os.path.join(td, "nb.ipynb")
469 with io.open(infile, 'w', encoding='utf-8') as f:
469 with io.open(infile, 'w', encoding='utf-8') as f:
470 current.write(nb0, f, 'json')
470 current.write(nb0, f, 'json')
471
471
472 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
472 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
473 _ip.magic("notebook -f py %s" % infile)
473 _ip.magic("notebook -f py %s" % infile)
474
474
475 def test_notebook_reformat_json():
475 def test_notebook_reformat_json():
476 with TemporaryDirectory() as td:
476 with TemporaryDirectory() as td:
477 infile = os.path.join(td, "nb.py")
477 infile = os.path.join(td, "nb.py")
478 with io.open(infile, 'w', encoding='utf-8') as f:
478 with io.open(infile, 'w', encoding='utf-8') as f:
479 current.write(nb0, f, 'py')
479 current.write(nb0, f, 'py')
480
480
481 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
481 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
482 _ip.magic("notebook -f ipynb %s" % infile)
482 _ip.magic("notebook -f ipynb %s" % infile)
483 _ip.magic("notebook -f json %s" % infile)
483 _ip.magic("notebook -f json %s" % infile)
484
484
485 def test_env():
485 def test_env():
486 env = _ip.magic("env")
486 env = _ip.magic("env")
487 assert isinstance(env, dict), type(env)
487 assert isinstance(env, dict), type(env)
@@ -1,536 +1,536 b''
1 """IPython extension to reload modules before executing user code.
1 """IPython extension to reload modules before executing user code.
2
2
3 ``autoreload`` reloads modules automatically before entering the execution of
3 ``autoreload`` reloads modules automatically before entering the execution of
4 code typed at the IPython prompt.
4 code typed at the IPython prompt.
5
5
6 This makes for example the following workflow possible:
6 This makes for example the following workflow possible:
7
7
8 .. sourcecode:: ipython
8 .. sourcecode:: ipython
9
9
10 In [1]: %load_ext autoreload
10 In [1]: %load_ext autoreload
11
11
12 In [2]: %autoreload 2
12 In [2]: %autoreload 2
13
13
14 In [3]: from foo import some_function
14 In [3]: from foo import some_function
15
15
16 In [4]: some_function()
16 In [4]: some_function()
17 Out[4]: 42
17 Out[4]: 42
18
18
19 In [5]: # open foo.py in an editor and change some_function to return 43
19 In [5]: # open foo.py in an editor and change some_function to return 43
20
20
21 In [6]: some_function()
21 In [6]: some_function()
22 Out[6]: 43
22 Out[6]: 43
23
23
24 The module was reloaded without reloading it explicitly, and the object
24 The module was reloaded without reloading it explicitly, and the object
25 imported with ``from foo import ...`` was also updated.
25 imported with ``from foo import ...`` was also updated.
26
26
27 Usage
27 Usage
28 =====
28 =====
29
29
30 The following magic commands are provided:
30 The following magic commands are provided:
31
31
32 ``%autoreload``
32 ``%autoreload``
33
33
34 Reload all modules (except those excluded by ``%aimport``)
34 Reload all modules (except those excluded by ``%aimport``)
35 automatically now.
35 automatically now.
36
36
37 ``%autoreload 0``
37 ``%autoreload 0``
38
38
39 Disable automatic reloading.
39 Disable automatic reloading.
40
40
41 ``%autoreload 1``
41 ``%autoreload 1``
42
42
43 Reload all modules imported with ``%aimport`` every time before
43 Reload all modules imported with ``%aimport`` every time before
44 executing the Python code typed.
44 executing the Python code typed.
45
45
46 ``%autoreload 2``
46 ``%autoreload 2``
47
47
48 Reload all modules (except those excluded by ``%aimport``) every
48 Reload all modules (except those excluded by ``%aimport``) every
49 time before executing the Python code typed.
49 time before executing the Python code typed.
50
50
51 ``%aimport``
51 ``%aimport``
52
52
53 List modules which are to be automatically imported or not to be imported.
53 List modules which are to be automatically imported or not to be imported.
54
54
55 ``%aimport foo``
55 ``%aimport foo``
56
56
57 Import module 'foo' and mark it to be autoreloaded for ``%autoreload 1``
57 Import module 'foo' and mark it to be autoreloaded for ``%autoreload 1``
58
58
59 ``%aimport -foo``
59 ``%aimport -foo``
60
60
61 Mark module 'foo' to not be autoreloaded.
61 Mark module 'foo' to not be autoreloaded.
62
62
63 Caveats
63 Caveats
64 =======
64 =======
65
65
66 Reloading Python modules in a reliable way is in general difficult,
66 Reloading Python modules in a reliable way is in general difficult,
67 and unexpected things may occur. ``%autoreload`` tries to work around
67 and unexpected things may occur. ``%autoreload`` tries to work around
68 common pitfalls by replacing function code objects and parts of
68 common pitfalls by replacing function code objects and parts of
69 classes previously in the module with new versions. This makes the
69 classes previously in the module with new versions. This makes the
70 following things to work:
70 following things to work:
71
71
72 - Functions and classes imported via 'from xxx import foo' are upgraded
72 - Functions and classes imported via 'from xxx import foo' are upgraded
73 to new versions when 'xxx' is reloaded.
73 to new versions when 'xxx' is reloaded.
74
74
75 - Methods and properties of classes are upgraded on reload, so that
75 - Methods and properties of classes are upgraded on reload, so that
76 calling 'c.foo()' on an object 'c' created before the reload causes
76 calling 'c.foo()' on an object 'c' created before the reload causes
77 the new code for 'foo' to be executed.
77 the new code for 'foo' to be executed.
78
78
79 Some of the known remaining caveats are:
79 Some of the known remaining caveats are:
80
80
81 - Replacing code objects does not always succeed: changing a @property
81 - Replacing code objects does not always succeed: changing a @property
82 in a class to an ordinary method or a method to a member variable
82 in a class to an ordinary method or a method to a member variable
83 can cause problems (but in old objects only).
83 can cause problems (but in old objects only).
84
84
85 - Functions that are removed (eg. via monkey-patching) from a module
85 - Functions that are removed (eg. via monkey-patching) from a module
86 before it is reloaded are not upgraded.
86 before it is reloaded are not upgraded.
87
87
88 - C extension modules cannot be reloaded, and so cannot be autoreloaded.
88 - C extension modules cannot be reloaded, and so cannot be autoreloaded.
89 """
89 """
90
90
91 skip_doctest = True
91 skip_doctest = True
92
92
93 #-----------------------------------------------------------------------------
93 #-----------------------------------------------------------------------------
94 # Copyright (C) 2000 Thomas Heller
94 # Copyright (C) 2000 Thomas Heller
95 # Copyright (C) 2008 Pauli Virtanen <pav@iki.fi>
95 # Copyright (C) 2008 Pauli Virtanen <pav@iki.fi>
96 # Copyright (C) 2012 The IPython Development Team
96 # Copyright (C) 2012 The IPython Development Team
97 #
97 #
98 # Distributed under the terms of the BSD License. The full license is in
98 # Distributed under the terms of the BSD License. The full license is in
99 # the file COPYING, distributed as part of this software.
99 # the file COPYING, distributed as part of this software.
100 #-----------------------------------------------------------------------------
100 #-----------------------------------------------------------------------------
101 #
101 #
102 # This IPython module is written by Pauli Virtanen, based on the autoreload
102 # This IPython module is written by Pauli Virtanen, based on the autoreload
103 # code by Thomas Heller.
103 # code by Thomas Heller.
104
104
105 #-----------------------------------------------------------------------------
105 #-----------------------------------------------------------------------------
106 # Imports
106 # Imports
107 #-----------------------------------------------------------------------------
107 #-----------------------------------------------------------------------------
108 import atexit
108 import atexit
109 import imp
109 import imp
110 import inspect
110 import inspect
111 import os
111 import os
112 import sys
112 import sys
113 import threading
113 import threading
114 import time
114 import time
115 import traceback
115 import traceback
116 import types
116 import types
117 import weakref
117 import weakref
118
118
119 try:
119 try:
120 # Reload is not defined by default in Python3.
120 # Reload is not defined by default in Python3.
121 reload
121 reload
122 except NameError:
122 except NameError:
123 from imp import reload
123 from imp import reload
124
124
125 from IPython.utils import pyfile
125 from IPython.utils import pyfile
126 from IPython.utils.py3compat import PY3
126 from IPython.utils.py3compat import PY3
127
127
128 #------------------------------------------------------------------------------
128 #------------------------------------------------------------------------------
129 # Autoreload functionality
129 # Autoreload functionality
130 #------------------------------------------------------------------------------
130 #------------------------------------------------------------------------------
131
131
132 def _get_compiled_ext():
132 def _get_compiled_ext():
133 """Official way to get the extension of compiled files (.pyc or .pyo)"""
133 """Official way to get the extension of compiled files (.pyc or .pyo)"""
134 for ext, mode, typ in imp.get_suffixes():
134 for ext, mode, typ in imp.get_suffixes():
135 if typ == imp.PY_COMPILED:
135 if typ == imp.PY_COMPILED:
136 return ext
136 return ext
137
137
138
138
139 PY_COMPILED_EXT = _get_compiled_ext()
139 PY_COMPILED_EXT = _get_compiled_ext()
140
140
141
141
142 class ModuleReloader(object):
142 class ModuleReloader(object):
143 enabled = False
143 enabled = False
144 """Whether this reloader is enabled"""
144 """Whether this reloader is enabled"""
145
145
146 failed = {}
146 failed = {}
147 """Modules that failed to reload: {module: mtime-on-failed-reload, ...}"""
147 """Modules that failed to reload: {module: mtime-on-failed-reload, ...}"""
148
148
149 modules = {}
149 modules = {}
150 """Modules specially marked as autoreloadable."""
150 """Modules specially marked as autoreloadable."""
151
151
152 skip_modules = {}
152 skip_modules = {}
153 """Modules specially marked as not autoreloadable."""
153 """Modules specially marked as not autoreloadable."""
154
154
155 check_all = True
155 check_all = True
156 """Autoreload all modules, not just those listed in 'modules'"""
156 """Autoreload all modules, not just those listed in 'modules'"""
157
157
158 old_objects = {}
158 old_objects = {}
159 """(module-name, name) -> weakref, for replacing old code objects"""
159 """(module-name, name) -> weakref, for replacing old code objects"""
160
160
161 def mark_module_skipped(self, module_name):
161 def mark_module_skipped(self, module_name):
162 """Skip reloading the named module in the future"""
162 """Skip reloading the named module in the future"""
163 try:
163 try:
164 del self.modules[module_name]
164 del self.modules[module_name]
165 except KeyError:
165 except KeyError:
166 pass
166 pass
167 self.skip_modules[module_name] = True
167 self.skip_modules[module_name] = True
168
168
169 def mark_module_reloadable(self, module_name):
169 def mark_module_reloadable(self, module_name):
170 """Reload the named module in the future (if it is imported)"""
170 """Reload the named module in the future (if it is imported)"""
171 try:
171 try:
172 del self.skip_modules[module_name]
172 del self.skip_modules[module_name]
173 except KeyError:
173 except KeyError:
174 pass
174 pass
175 self.modules[module_name] = True
175 self.modules[module_name] = True
176
176
177 def aimport_module(self, module_name):
177 def aimport_module(self, module_name):
178 """Import a module, and mark it reloadable
178 """Import a module, and mark it reloadable
179
179
180 Returns
180 Returns
181 -------
181 -------
182 top_module : module
182 top_module : module
183 The imported module if it is top-level, or the top-level
183 The imported module if it is top-level, or the top-level
184 top_name : module
184 top_name : module
185 Name of top_module
185 Name of top_module
186
186
187 """
187 """
188 self.mark_module_reloadable(module_name)
188 self.mark_module_reloadable(module_name)
189
189
190 __import__(module_name)
190 __import__(module_name)
191 top_name = module_name.split('.')[0]
191 top_name = module_name.split('.')[0]
192 top_module = sys.modules[top_name]
192 top_module = sys.modules[top_name]
193 return top_module, top_name
193 return top_module, top_name
194
194
195 def check(self, check_all=False):
195 def check(self, check_all=False):
196 """Check whether some modules need to be reloaded."""
196 """Check whether some modules need to be reloaded."""
197
197
198 if not self.enabled and not check_all:
198 if not self.enabled and not check_all:
199 return
199 return
200
200
201 if check_all or self.check_all:
201 if check_all or self.check_all:
202 modules = sys.modules.keys()
202 modules = sys.modules.keys()
203 else:
203 else:
204 modules = self.modules.keys()
204 modules = self.modules.keys()
205
205
206 for modname in modules:
206 for modname in modules:
207 m = sys.modules.get(modname, None)
207 m = sys.modules.get(modname, None)
208
208
209 if modname in self.skip_modules:
209 if modname in self.skip_modules:
210 continue
210 continue
211
211
212 if not hasattr(m, '__file__'):
212 if not hasattr(m, '__file__'):
213 continue
213 continue
214
214
215 if m.__name__ == '__main__':
215 if m.__name__ == '__main__':
216 # we cannot reload(__main__)
216 # we cannot reload(__main__)
217 continue
217 continue
218
218
219 filename = m.__file__
219 filename = m.__file__
220 path, ext = os.path.splitext(filename)
220 path, ext = os.path.splitext(filename)
221
221
222 if ext.lower() == '.py':
222 if ext.lower() == '.py':
223 ext = PY_COMPILED_EXT
223 ext = PY_COMPILED_EXT
224 pyc_filename = pyfile.cache_from_source(filename)
224 pyc_filename = pyfile.cache_from_source(filename)
225 py_filename = filename
225 py_filename = filename
226 else:
226 else:
227 pyc_filename = filename
227 pyc_filename = filename
228 try:
228 try:
229 py_filename = pyfile.source_from_cache(filename)
229 py_filename = pyfile.source_from_cache(filename)
230 except ValueError:
230 except ValueError:
231 continue
231 continue
232
232
233 try:
233 try:
234 pymtime = os.stat(py_filename).st_mtime
234 pymtime = os.stat(py_filename).st_mtime
235 if pymtime <= os.stat(pyc_filename).st_mtime:
235 if pymtime <= os.stat(pyc_filename).st_mtime:
236 continue
236 continue
237 if self.failed.get(py_filename, None) == pymtime:
237 if self.failed.get(py_filename, None) == pymtime:
238 continue
238 continue
239 except OSError:
239 except OSError:
240 continue
240 continue
241
241
242 try:
242 try:
243 superreload(m, reload, self.old_objects)
243 superreload(m, reload, self.old_objects)
244 if py_filename in self.failed:
244 if py_filename in self.failed:
245 del self.failed[py_filename]
245 del self.failed[py_filename]
246 except:
246 except:
247 print >> sys.stderr, "[autoreload of %s failed: %s]" % (
247 print >> sys.stderr, "[autoreload of %s failed: %s]" % (
248 modname, traceback.format_exc(1))
248 modname, traceback.format_exc(1))
249 self.failed[py_filename] = pymtime
249 self.failed[py_filename] = pymtime
250
250
251 #------------------------------------------------------------------------------
251 #------------------------------------------------------------------------------
252 # superreload
252 # superreload
253 #------------------------------------------------------------------------------
253 #------------------------------------------------------------------------------
254
254
255 if PY3:
255 if PY3:
256 func_attrs = ['__code__', '__defaults__', '__doc__',
256 func_attrs = ['__code__', '__defaults__', '__doc__',
257 '__closure__', '__globals__', '__dict__']
257 '__closure__', '__globals__', '__dict__']
258 else:
258 else:
259 func_attrs = ['func_code', 'func_defaults', 'func_doc',
259 func_attrs = ['func_code', 'func_defaults', 'func_doc',
260 'func_closure', 'func_globals', 'func_dict']
260 'func_closure', 'func_globals', 'func_dict']
261
261
262
262
263 def update_function(old, new):
263 def update_function(old, new):
264 """Upgrade the code object of a function"""
264 """Upgrade the code object of a function"""
265 for name in func_attrs:
265 for name in func_attrs:
266 try:
266 try:
267 setattr(old, name, getattr(new, name))
267 setattr(old, name, getattr(new, name))
268 except (AttributeError, TypeError):
268 except (AttributeError, TypeError):
269 pass
269 pass
270
270
271
271
272 def update_class(old, new):
272 def update_class(old, new):
273 """Replace stuff in the __dict__ of a class, and upgrade
273 """Replace stuff in the __dict__ of a class, and upgrade
274 method code objects"""
274 method code objects"""
275 for key in old.__dict__.keys():
275 for key in old.__dict__.keys():
276 old_obj = getattr(old, key)
276 old_obj = getattr(old, key)
277
277
278 try:
278 try:
279 new_obj = getattr(new, key)
279 new_obj = getattr(new, key)
280 except AttributeError:
280 except AttributeError:
281 # obsolete attribute: remove it
281 # obsolete attribute: remove it
282 try:
282 try:
283 delattr(old, key)
283 delattr(old, key)
284 except (AttributeError, TypeError):
284 except (AttributeError, TypeError):
285 pass
285 pass
286 continue
286 continue
287
287
288 if update_generic(old_obj, new_obj): continue
288 if update_generic(old_obj, new_obj): continue
289
289
290 try:
290 try:
291 setattr(old, key, getattr(new, key))
291 setattr(old, key, getattr(new, key))
292 except (AttributeError, TypeError):
292 except (AttributeError, TypeError):
293 pass # skip non-writable attributes
293 pass # skip non-writable attributes
294
294
295
295
296 def update_property(old, new):
296 def update_property(old, new):
297 """Replace get/set/del functions of a property"""
297 """Replace get/set/del functions of a property"""
298 update_generic(old.fdel, new.fdel)
298 update_generic(old.fdel, new.fdel)
299 update_generic(old.fget, new.fget)
299 update_generic(old.fget, new.fget)
300 update_generic(old.fset, new.fset)
300 update_generic(old.fset, new.fset)
301
301
302
302
303 def isinstance2(a, b, typ):
303 def isinstance2(a, b, typ):
304 return isinstance(a, typ) and isinstance(b, typ)
304 return isinstance(a, typ) and isinstance(b, typ)
305
305
306
306
307 UPDATE_RULES = [
307 UPDATE_RULES = [
308 (lambda a, b: isinstance2(a, b, type),
308 (lambda a, b: isinstance2(a, b, type),
309 update_class),
309 update_class),
310 (lambda a, b: isinstance2(a, b, types.FunctionType),
310 (lambda a, b: isinstance2(a, b, types.FunctionType),
311 update_function),
311 update_function),
312 (lambda a, b: isinstance2(a, b, property),
312 (lambda a, b: isinstance2(a, b, property),
313 update_property),
313 update_property),
314 ]
314 ]
315
315
316
316
317 if PY3:
317 if PY3:
318 UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.MethodType),
318 UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.MethodType),
319 lambda a, b: update_function(a.__func__, b.__func__)),
319 lambda a, b: update_function(a.__func__, b.__func__)),
320 ])
320 ])
321 else:
321 else:
322 UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.ClassType),
322 UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.ClassType),
323 update_class),
323 update_class),
324 (lambda a, b: isinstance2(a, b, types.MethodType),
324 (lambda a, b: isinstance2(a, b, types.MethodType),
325 lambda a, b: update_function(a.im_func, b.im_func)),
325 lambda a, b: update_function(a.im_func, b.im_func)),
326 ])
326 ])
327
327
328
328
329 def update_generic(a, b):
329 def update_generic(a, b):
330 for type_check, update in UPDATE_RULES:
330 for type_check, update in UPDATE_RULES:
331 if type_check(a, b):
331 if type_check(a, b):
332 update(a, b)
332 update(a, b)
333 return True
333 return True
334 return False
334 return False
335
335
336
336
337 class StrongRef(object):
337 class StrongRef(object):
338 def __init__(self, obj):
338 def __init__(self, obj):
339 self.obj = obj
339 self.obj = obj
340 def __call__(self):
340 def __call__(self):
341 return self.obj
341 return self.obj
342
342
343
343
344 def superreload(module, reload=reload, old_objects={}):
344 def superreload(module, reload=reload, old_objects={}):
345 """Enhanced version of the builtin reload function.
345 """Enhanced version of the builtin reload function.
346
346
347 superreload remembers objects previously in the module, and
347 superreload remembers objects previously in the module, and
348
348
349 - upgrades the class dictionary of every old class in the module
349 - upgrades the class dictionary of every old class in the module
350 - upgrades the code object of every old function and method
350 - upgrades the code object of every old function and method
351 - clears the module's namespace before reloading
351 - clears the module's namespace before reloading
352
352
353 """
353 """
354
354
355 # collect old objects in the module
355 # collect old objects in the module
356 for name, obj in module.__dict__.items():
356 for name, obj in module.__dict__.items():
357 if not hasattr(obj, '__module__') or obj.__module__ != module.__name__:
357 if not hasattr(obj, '__module__') or obj.__module__ != module.__name__:
358 continue
358 continue
359 key = (module.__name__, name)
359 key = (module.__name__, name)
360 try:
360 try:
361 old_objects.setdefault(key, []).append(weakref.ref(obj))
361 old_objects.setdefault(key, []).append(weakref.ref(obj))
362 except TypeError:
362 except TypeError:
363 # weakref doesn't work for all types;
363 # weakref doesn't work for all types;
364 # create strong references for 'important' cases
364 # create strong references for 'important' cases
365 if not PY3 and isinstance(obj, types.ClassType):
365 if not PY3 and isinstance(obj, types.ClassType):
366 old_objects.setdefault(key, []).append(StrongRef(obj))
366 old_objects.setdefault(key, []).append(StrongRef(obj))
367
367
368 # reload module
368 # reload module
369 try:
369 try:
370 # clear namespace first from old cruft
370 # clear namespace first from old cruft
371 old_dict = module.__dict__.copy()
371 old_dict = module.__dict__.copy()
372 old_name = module.__name__
372 old_name = module.__name__
373 module.__dict__.clear()
373 module.__dict__.clear()
374 module.__dict__['__name__'] = old_name
374 module.__dict__['__name__'] = old_name
375 except (TypeError, AttributeError, KeyError):
375 except (TypeError, AttributeError, KeyError):
376 pass
376 pass
377
377
378 try:
378 try:
379 module = reload(module)
379 module = reload(module)
380 except:
380 except:
381 # restore module dictionary on failed reload
381 # restore module dictionary on failed reload
382 module.__dict__.update(old_dict)
382 module.__dict__.update(old_dict)
383 raise
383 raise
384
384
385 # iterate over all objects and update functions & classes
385 # iterate over all objects and update functions & classes
386 for name, new_obj in module.__dict__.items():
386 for name, new_obj in module.__dict__.items():
387 key = (module.__name__, name)
387 key = (module.__name__, name)
388 if key not in old_objects: continue
388 if key not in old_objects: continue
389
389
390 new_refs = []
390 new_refs = []
391 for old_ref in old_objects[key]:
391 for old_ref in old_objects[key]:
392 old_obj = old_ref()
392 old_obj = old_ref()
393 if old_obj is None: continue
393 if old_obj is None: continue
394 new_refs.append(old_ref)
394 new_refs.append(old_ref)
395 update_generic(old_obj, new_obj)
395 update_generic(old_obj, new_obj)
396
396
397 if new_refs:
397 if new_refs:
398 old_objects[key] = new_refs
398 old_objects[key] = new_refs
399 else:
399 else:
400 del old_objects[key]
400 del old_objects[key]
401
401
402 return module
402 return module
403
403
404 #------------------------------------------------------------------------------
404 #------------------------------------------------------------------------------
405 # IPython connectivity
405 # IPython connectivity
406 #------------------------------------------------------------------------------
406 #------------------------------------------------------------------------------
407
407
408 from IPython.core.hooks import TryNext
408 from IPython.core.hooks import TryNext
409 from IPython.core.magic import Magics, register_magics, line_magic
409 from IPython.core.magic import Magics, magics_class, line_magic
410 from IPython.core.plugin import Plugin
410 from IPython.core.plugin import Plugin
411
411
412 @register_magics
412 @magics_class
413 class AutoreloadMagics(Magics):
413 class AutoreloadMagics(Magics):
414 def __init__(self, *a, **kw):
414 def __init__(self, *a, **kw):
415 super(AutoreloadMagics, self).__init__(*a, **kw)
415 super(AutoreloadMagics, self).__init__(*a, **kw)
416 self._reloader = ModuleReloader()
416 self._reloader = ModuleReloader()
417 self._reloader.check_all = False
417 self._reloader.check_all = False
418
418
419 @line_magic
419 @line_magic
420 def autoreload(self, parameter_s=''):
420 def autoreload(self, parameter_s=''):
421 r"""%autoreload => Reload modules automatically
421 r"""%autoreload => Reload modules automatically
422
422
423 %autoreload
423 %autoreload
424 Reload all modules (except those excluded by %aimport) automatically
424 Reload all modules (except those excluded by %aimport) automatically
425 now.
425 now.
426
426
427 %autoreload 0
427 %autoreload 0
428 Disable automatic reloading.
428 Disable automatic reloading.
429
429
430 %autoreload 1
430 %autoreload 1
431 Reload all modules imported with %aimport every time before executing
431 Reload all modules imported with %aimport every time before executing
432 the Python code typed.
432 the Python code typed.
433
433
434 %autoreload 2
434 %autoreload 2
435 Reload all modules (except those excluded by %aimport) every time
435 Reload all modules (except those excluded by %aimport) every time
436 before executing the Python code typed.
436 before executing the Python code typed.
437
437
438 Reloading Python modules in a reliable way is in general
438 Reloading Python modules in a reliable way is in general
439 difficult, and unexpected things may occur. %autoreload tries to
439 difficult, and unexpected things may occur. %autoreload tries to
440 work around common pitfalls by replacing function code objects and
440 work around common pitfalls by replacing function code objects and
441 parts of classes previously in the module with new versions. This
441 parts of classes previously in the module with new versions. This
442 makes the following things to work:
442 makes the following things to work:
443
443
444 - Functions and classes imported via 'from xxx import foo' are upgraded
444 - Functions and classes imported via 'from xxx import foo' are upgraded
445 to new versions when 'xxx' is reloaded.
445 to new versions when 'xxx' is reloaded.
446
446
447 - Methods and properties of classes are upgraded on reload, so that
447 - Methods and properties of classes are upgraded on reload, so that
448 calling 'c.foo()' on an object 'c' created before the reload causes
448 calling 'c.foo()' on an object 'c' created before the reload causes
449 the new code for 'foo' to be executed.
449 the new code for 'foo' to be executed.
450
450
451 Some of the known remaining caveats are:
451 Some of the known remaining caveats are:
452
452
453 - Replacing code objects does not always succeed: changing a @property
453 - Replacing code objects does not always succeed: changing a @property
454 in a class to an ordinary method or a method to a member variable
454 in a class to an ordinary method or a method to a member variable
455 can cause problems (but in old objects only).
455 can cause problems (but in old objects only).
456
456
457 - Functions that are removed (eg. via monkey-patching) from a module
457 - Functions that are removed (eg. via monkey-patching) from a module
458 before it is reloaded are not upgraded.
458 before it is reloaded are not upgraded.
459
459
460 - C extension modules cannot be reloaded, and so cannot be
460 - C extension modules cannot be reloaded, and so cannot be
461 autoreloaded.
461 autoreloaded.
462
462
463 """
463 """
464 if parameter_s == '':
464 if parameter_s == '':
465 self._reloader.check(True)
465 self._reloader.check(True)
466 elif parameter_s == '0':
466 elif parameter_s == '0':
467 self._reloader.enabled = False
467 self._reloader.enabled = False
468 elif parameter_s == '1':
468 elif parameter_s == '1':
469 self._reloader.check_all = False
469 self._reloader.check_all = False
470 self._reloader.enabled = True
470 self._reloader.enabled = True
471 elif parameter_s == '2':
471 elif parameter_s == '2':
472 self._reloader.check_all = True
472 self._reloader.check_all = True
473 self._reloader.enabled = True
473 self._reloader.enabled = True
474
474
475 @line_magic
475 @line_magic
476 def aimport(self, parameter_s='', stream=None):
476 def aimport(self, parameter_s='', stream=None):
477 """%aimport => Import modules for automatic reloading.
477 """%aimport => Import modules for automatic reloading.
478
478
479 %aimport
479 %aimport
480 List modules to automatically import and not to import.
480 List modules to automatically import and not to import.
481
481
482 %aimport foo
482 %aimport foo
483 Import module 'foo' and mark it to be autoreloaded for %autoreload 1
483 Import module 'foo' and mark it to be autoreloaded for %autoreload 1
484
484
485 %aimport -foo
485 %aimport -foo
486 Mark module 'foo' to not be autoreloaded for %autoreload 1
486 Mark module 'foo' to not be autoreloaded for %autoreload 1
487 """
487 """
488 modname = parameter_s
488 modname = parameter_s
489 if not modname:
489 if not modname:
490 to_reload = self._reloader.modules.keys()
490 to_reload = self._reloader.modules.keys()
491 to_reload.sort()
491 to_reload.sort()
492 to_skip = self._reloader.skip_modules.keys()
492 to_skip = self._reloader.skip_modules.keys()
493 to_skip.sort()
493 to_skip.sort()
494 if stream is None:
494 if stream is None:
495 stream = sys.stdout
495 stream = sys.stdout
496 if self._reloader.check_all:
496 if self._reloader.check_all:
497 stream.write("Modules to reload:\nall-except-skipped\n")
497 stream.write("Modules to reload:\nall-except-skipped\n")
498 else:
498 else:
499 stream.write("Modules to reload:\n%s\n" % ' '.join(to_reload))
499 stream.write("Modules to reload:\n%s\n" % ' '.join(to_reload))
500 stream.write("\nModules to skip:\n%s\n" % ' '.join(to_skip))
500 stream.write("\nModules to skip:\n%s\n" % ' '.join(to_skip))
501 elif modname.startswith('-'):
501 elif modname.startswith('-'):
502 modname = modname[1:]
502 modname = modname[1:]
503 self._reloader.mark_module_skipped(modname)
503 self._reloader.mark_module_skipped(modname)
504 else:
504 else:
505 top_module, top_name = self._reloader.aimport_module(modname)
505 top_module, top_name = self._reloader.aimport_module(modname)
506
506
507 # Inject module to user namespace
507 # Inject module to user namespace
508 self.shell.push({top_name: top_module})
508 self.shell.push({top_name: top_module})
509
509
510 def pre_run_code_hook(self, ip):
510 def pre_run_code_hook(self, ip):
511 if not self._reloader.enabled:
511 if not self._reloader.enabled:
512 raise TryNext
512 raise TryNext
513 try:
513 try:
514 self._reloader.check()
514 self._reloader.check()
515 except:
515 except:
516 pass
516 pass
517
517
518
518
519 class AutoreloadPlugin(Plugin):
519 class AutoreloadPlugin(Plugin):
520 def __init__(self, shell=None, config=None):
520 def __init__(self, shell=None, config=None):
521 super(AutoreloadPlugin, self).__init__(shell=shell, config=config)
521 super(AutoreloadPlugin, self).__init__(shell=shell, config=config)
522 self.auto_magics = AutoreloadMagics(shell)
522 self.auto_magics = AutoreloadMagics(shell)
523 shell.register_magics(self.auto_magics)
523 shell.register_magics(self.auto_magics)
524 shell.set_hook('pre_run_code_hook', self.auto_magics.pre_run_code_hook)
524 shell.set_hook('pre_run_code_hook', self.auto_magics.pre_run_code_hook)
525
525
526
526
527 _loaded = False
527 _loaded = False
528
528
529
529
530 def load_ipython_extension(ip):
530 def load_ipython_extension(ip):
531 """Load the extension in IPython."""
531 """Load the extension in IPython."""
532 global _loaded
532 global _loaded
533 if not _loaded:
533 if not _loaded:
534 plugin = AutoreloadPlugin(shell=ip, config=ip.config)
534 plugin = AutoreloadPlugin(shell=ip, config=ip.config)
535 ip.plugin_manager.register_plugin('autoreload', plugin)
535 ip.plugin_manager.register_plugin('autoreload', plugin)
536 _loaded = True
536 _loaded = True
@@ -1,316 +1,316 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 =============
3 =============
4 parallelmagic
4 parallelmagic
5 =============
5 =============
6
6
7 Magic command interface for interactive parallel work.
7 Magic command interface for interactive parallel work.
8
8
9 Usage
9 Usage
10 =====
10 =====
11
11
12 ``%autopx``
12 ``%autopx``
13
13
14 @AUTOPX_DOC@
14 @AUTOPX_DOC@
15
15
16 ``%px``
16 ``%px``
17
17
18 @PX_DOC@
18 @PX_DOC@
19
19
20 ``%result``
20 ``%result``
21
21
22 @RESULT_DOC@
22 @RESULT_DOC@
23
23
24 """
24 """
25
25
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Copyright (C) 2008 The IPython Development Team
27 # Copyright (C) 2008 The IPython Development Team
28 #
28 #
29 # Distributed under the terms of the BSD License. The full license is in
29 # Distributed under the terms of the BSD License. The full license is in
30 # the file COPYING, distributed as part of this software.
30 # the file COPYING, distributed as part of this software.
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 #-----------------------------------------------------------------------------
33 #-----------------------------------------------------------------------------
34 # Imports
34 # Imports
35 #-----------------------------------------------------------------------------
35 #-----------------------------------------------------------------------------
36
36
37 import ast
37 import ast
38 import re
38 import re
39
39
40 from IPython.core.magic import Magics, register_magics, line_magic
40 from IPython.core.magic import Magics, magics_class, line_magic
41 from IPython.testing.skipdoctest import skip_doctest
41 from IPython.testing.skipdoctest import skip_doctest
42
42
43 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
44 # Definitions of magic functions for use with IPython
44 # Definitions of magic functions for use with IPython
45 #-----------------------------------------------------------------------------
45 #-----------------------------------------------------------------------------
46
46
47 NO_ACTIVE_VIEW = """
47 NO_ACTIVE_VIEW = """
48 Use activate() on a DirectView object to activate it for magics.
48 Use activate() on a DirectView object to activate it for magics.
49 """
49 """
50
50
51
51
52 @register_magics
52 @magics_class
53 class ParallelMagics(Magics):
53 class ParallelMagics(Magics):
54 """A set of magics useful when controlling a parallel IPython cluster.
54 """A set of magics useful when controlling a parallel IPython cluster.
55 """
55 """
56
56
57 def __init__(self, shell):
57 def __init__(self, shell):
58 super(ParallelMagics, self).__init__(shell)
58 super(ParallelMagics, self).__init__(shell)
59 # A flag showing if autopx is activated or not
59 # A flag showing if autopx is activated or not
60 self.autopx = False
60 self.autopx = False
61
61
62 @skip_doctest
62 @skip_doctest
63 @line_magic
63 @line_magic
64 def result(self, parameter_s=''):
64 def result(self, parameter_s=''):
65 """Print the result of command i on all engines..
65 """Print the result of command i on all engines..
66
66
67 To use this a :class:`DirectView` instance must be created
67 To use this a :class:`DirectView` instance must be created
68 and then activated by calling its :meth:`activate` method.
68 and then activated by calling its :meth:`activate` method.
69
69
70 Then you can do the following::
70 Then you can do the following::
71
71
72 In [23]: %result
72 In [23]: %result
73 Out[23]:
73 Out[23]:
74 <Results List>
74 <Results List>
75 [0] In [6]: a = 10
75 [0] In [6]: a = 10
76 [1] In [6]: a = 10
76 [1] In [6]: a = 10
77
77
78 In [22]: %result 6
78 In [22]: %result 6
79 Out[22]:
79 Out[22]:
80 <Results List>
80 <Results List>
81 [0] In [6]: a = 10
81 [0] In [6]: a = 10
82 [1] In [6]: a = 10
82 [1] In [6]: a = 10
83 """
83 """
84 if self.active_view is None:
84 if self.active_view is None:
85 print NO_ACTIVE_VIEW
85 print NO_ACTIVE_VIEW
86 return
86 return
87
87
88 try:
88 try:
89 index = int(parameter_s)
89 index = int(parameter_s)
90 except:
90 except:
91 index = None
91 index = None
92 result = self.active_view.get_result(index)
92 result = self.active_view.get_result(index)
93 return result
93 return result
94
94
95 @skip_doctest
95 @skip_doctest
96 @line_magic
96 @line_magic
97 def px(self, parameter_s=''):
97 def px(self, parameter_s=''):
98 """Executes the given python command in parallel.
98 """Executes the given python command in parallel.
99
99
100 To use this a :class:`DirectView` instance must be created
100 To use this a :class:`DirectView` instance must be created
101 and then activated by calling its :meth:`activate` method.
101 and then activated by calling its :meth:`activate` method.
102
102
103 Then you can do the following::
103 Then you can do the following::
104
104
105 In [24]: %px a = 5
105 In [24]: %px a = 5
106 Parallel execution on engine(s): all
106 Parallel execution on engine(s): all
107 Out[24]:
107 Out[24]:
108 <Results List>
108 <Results List>
109 [0] In [7]: a = 5
109 [0] In [7]: a = 5
110 [1] In [7]: a = 5
110 [1] In [7]: a = 5
111 """
111 """
112
112
113 if self.active_view is None:
113 if self.active_view is None:
114 print NO_ACTIVE_VIEW
114 print NO_ACTIVE_VIEW
115 return
115 return
116 print "Parallel execution on engine(s): %s" % self.active_view.targets
116 print "Parallel execution on engine(s): %s" % self.active_view.targets
117 result = self.active_view.execute(parameter_s, block=False)
117 result = self.active_view.execute(parameter_s, block=False)
118 if self.active_view.block:
118 if self.active_view.block:
119 result.get()
119 result.get()
120 self._maybe_display_output(result)
120 self._maybe_display_output(result)
121
121
122 @skip_doctest
122 @skip_doctest
123 @line_magic
123 @line_magic
124 def autopx(self, parameter_s=''):
124 def autopx(self, parameter_s=''):
125 """Toggles auto parallel mode.
125 """Toggles auto parallel mode.
126
126
127 To use this a :class:`DirectView` instance must be created
127 To use this a :class:`DirectView` instance must be created
128 and then activated by calling its :meth:`activate` method. Once this
128 and then activated by calling its :meth:`activate` method. Once this
129 is called, all commands typed at the command line are send to
129 is called, all commands typed at the command line are send to
130 the engines to be executed in parallel. To control which engine
130 the engines to be executed in parallel. To control which engine
131 are used, set the ``targets`` attributed of the multiengine client
131 are used, set the ``targets`` attributed of the multiengine client
132 before entering ``%autopx`` mode.
132 before entering ``%autopx`` mode.
133
133
134 Then you can do the following::
134 Then you can do the following::
135
135
136 In [25]: %autopx
136 In [25]: %autopx
137 %autopx to enabled
137 %autopx to enabled
138
138
139 In [26]: a = 10
139 In [26]: a = 10
140 Parallel execution on engine(s): [0,1,2,3]
140 Parallel execution on engine(s): [0,1,2,3]
141 In [27]: print a
141 In [27]: print a
142 Parallel execution on engine(s): [0,1,2,3]
142 Parallel execution on engine(s): [0,1,2,3]
143 [stdout:0] 10
143 [stdout:0] 10
144 [stdout:1] 10
144 [stdout:1] 10
145 [stdout:2] 10
145 [stdout:2] 10
146 [stdout:3] 10
146 [stdout:3] 10
147
147
148
148
149 In [27]: %autopx
149 In [27]: %autopx
150 %autopx disabled
150 %autopx disabled
151 """
151 """
152 if self.autopx:
152 if self.autopx:
153 self._disable_autopx()
153 self._disable_autopx()
154 else:
154 else:
155 self._enable_autopx()
155 self._enable_autopx()
156
156
157 def _enable_autopx(self):
157 def _enable_autopx(self):
158 """Enable %autopx mode by saving the original run_cell and installing
158 """Enable %autopx mode by saving the original run_cell and installing
159 pxrun_cell.
159 pxrun_cell.
160 """
160 """
161 if self.active_view is None:
161 if self.active_view is None:
162 print NO_ACTIVE_VIEW
162 print NO_ACTIVE_VIEW
163 return
163 return
164
164
165 # override run_cell and run_code
165 # override run_cell and run_code
166 self._original_run_cell = self.shell.run_cell
166 self._original_run_cell = self.shell.run_cell
167 self.shell.run_cell = self.pxrun_cell
167 self.shell.run_cell = self.pxrun_cell
168 self._original_run_code = self.shell.run_code
168 self._original_run_code = self.shell.run_code
169 self.shell.run_code = self.pxrun_code
169 self.shell.run_code = self.pxrun_code
170
170
171 self.autopx = True
171 self.autopx = True
172 print "%autopx enabled"
172 print "%autopx enabled"
173
173
174 def _disable_autopx(self):
174 def _disable_autopx(self):
175 """Disable %autopx by restoring the original InteractiveShell.run_cell.
175 """Disable %autopx by restoring the original InteractiveShell.run_cell.
176 """
176 """
177 if self.autopx:
177 if self.autopx:
178 self.shell.run_cell = self._original_run_cell
178 self.shell.run_cell = self._original_run_cell
179 self.shell.run_code = self._original_run_code
179 self.shell.run_code = self._original_run_code
180 self.autopx = False
180 self.autopx = False
181 print "%autopx disabled"
181 print "%autopx disabled"
182
182
183 def _maybe_display_output(self, result):
183 def _maybe_display_output(self, result):
184 """Maybe display the output of a parallel result.
184 """Maybe display the output of a parallel result.
185
185
186 If self.active_view.block is True, wait for the result
186 If self.active_view.block is True, wait for the result
187 and display the result. Otherwise, this is a noop.
187 and display the result. Otherwise, this is a noop.
188 """
188 """
189 if isinstance(result.stdout, basestring):
189 if isinstance(result.stdout, basestring):
190 # single result
190 # single result
191 stdouts = [result.stdout.rstrip()]
191 stdouts = [result.stdout.rstrip()]
192 else:
192 else:
193 stdouts = [s.rstrip() for s in result.stdout]
193 stdouts = [s.rstrip() for s in result.stdout]
194
194
195 targets = self.active_view.targets
195 targets = self.active_view.targets
196 if isinstance(targets, int):
196 if isinstance(targets, int):
197 targets = [targets]
197 targets = [targets]
198 elif targets == 'all':
198 elif targets == 'all':
199 targets = self.active_view.client.ids
199 targets = self.active_view.client.ids
200
200
201 if any(stdouts):
201 if any(stdouts):
202 for eid,stdout in zip(targets, stdouts):
202 for eid,stdout in zip(targets, stdouts):
203 print '[stdout:%i]'%eid, stdout
203 print '[stdout:%i]'%eid, stdout
204
204
205
205
206 def pxrun_cell(self, raw_cell, store_history=False, silent=False):
206 def pxrun_cell(self, raw_cell, store_history=False, silent=False):
207 """drop-in replacement for InteractiveShell.run_cell.
207 """drop-in replacement for InteractiveShell.run_cell.
208
208
209 This executes code remotely, instead of in the local namespace.
209 This executes code remotely, instead of in the local namespace.
210
210
211 See InteractiveShell.run_cell for details.
211 See InteractiveShell.run_cell for details.
212 """
212 """
213
213
214 if (not raw_cell) or raw_cell.isspace():
214 if (not raw_cell) or raw_cell.isspace():
215 return
215 return
216
216
217 ipself = self.shell
217 ipself = self.shell
218
218
219 with ipself.builtin_trap:
219 with ipself.builtin_trap:
220 cell = ipself.prefilter_manager.prefilter_lines(raw_cell)
220 cell = ipself.prefilter_manager.prefilter_lines(raw_cell)
221
221
222 # Store raw and processed history
222 # Store raw and processed history
223 if store_history:
223 if store_history:
224 ipself.history_manager.store_inputs(ipself.execution_count,
224 ipself.history_manager.store_inputs(ipself.execution_count,
225 cell, raw_cell)
225 cell, raw_cell)
226
226
227 # ipself.logger.log(cell, raw_cell)
227 # ipself.logger.log(cell, raw_cell)
228
228
229 cell_name = ipself.compile.cache(cell, ipself.execution_count)
229 cell_name = ipself.compile.cache(cell, ipself.execution_count)
230
230
231 try:
231 try:
232 ast.parse(cell, filename=cell_name)
232 ast.parse(cell, filename=cell_name)
233 except (OverflowError, SyntaxError, ValueError, TypeError,
233 except (OverflowError, SyntaxError, ValueError, TypeError,
234 MemoryError):
234 MemoryError):
235 # Case 1
235 # Case 1
236 ipself.showsyntaxerror()
236 ipself.showsyntaxerror()
237 ipself.execution_count += 1
237 ipself.execution_count += 1
238 return None
238 return None
239 except NameError:
239 except NameError:
240 # ignore name errors, because we don't know the remote keys
240 # ignore name errors, because we don't know the remote keys
241 pass
241 pass
242
242
243 if store_history:
243 if store_history:
244 # Write output to the database. Does nothing unless
244 # Write output to the database. Does nothing unless
245 # history output logging is enabled.
245 # history output logging is enabled.
246 ipself.history_manager.store_output(ipself.execution_count)
246 ipself.history_manager.store_output(ipself.execution_count)
247 # Each cell is a *single* input, regardless of how many lines it has
247 # Each cell is a *single* input, regardless of how many lines it has
248 ipself.execution_count += 1
248 ipself.execution_count += 1
249 if re.search(r'get_ipython\(\)\.magic\(u?["\']%?autopx', cell):
249 if re.search(r'get_ipython\(\)\.magic\(u?["\']%?autopx', cell):
250 self._disable_autopx()
250 self._disable_autopx()
251 return False
251 return False
252 else:
252 else:
253 try:
253 try:
254 result = self.active_view.execute(cell, silent=False, block=False)
254 result = self.active_view.execute(cell, silent=False, block=False)
255 except:
255 except:
256 ipself.showtraceback()
256 ipself.showtraceback()
257 return True
257 return True
258 else:
258 else:
259 if self.active_view.block:
259 if self.active_view.block:
260 try:
260 try:
261 result.get()
261 result.get()
262 except:
262 except:
263 self.shell.showtraceback()
263 self.shell.showtraceback()
264 return True
264 return True
265 else:
265 else:
266 self._maybe_display_output(result)
266 self._maybe_display_output(result)
267 return False
267 return False
268
268
269 def pxrun_code(self, code_obj):
269 def pxrun_code(self, code_obj):
270 """drop-in replacement for InteractiveShell.run_code.
270 """drop-in replacement for InteractiveShell.run_code.
271
271
272 This executes code remotely, instead of in the local namespace.
272 This executes code remotely, instead of in the local namespace.
273
273
274 See InteractiveShell.run_code for details.
274 See InteractiveShell.run_code for details.
275 """
275 """
276 ipself = self.shell
276 ipself = self.shell
277 # check code object for the autopx magic
277 # check code object for the autopx magic
278 if 'get_ipython' in code_obj.co_names and 'magic' in code_obj.co_names \
278 if 'get_ipython' in code_obj.co_names and 'magic' in code_obj.co_names \
279 and any( [ isinstance(c, basestring) and 'autopx' in c
279 and any( [ isinstance(c, basestring) and 'autopx' in c
280 for c in code_obj.co_consts ]):
280 for c in code_obj.co_consts ]):
281 self._disable_autopx()
281 self._disable_autopx()
282 return False
282 return False
283 else:
283 else:
284 try:
284 try:
285 result = self.active_view.execute(code_obj, block=False)
285 result = self.active_view.execute(code_obj, block=False)
286 except:
286 except:
287 ipself.showtraceback()
287 ipself.showtraceback()
288 return True
288 return True
289 else:
289 else:
290 if self.active_view.block:
290 if self.active_view.block:
291 try:
291 try:
292 result.get()
292 result.get()
293 except:
293 except:
294 self.shell.showtraceback()
294 self.shell.showtraceback()
295 return True
295 return True
296 else:
296 else:
297 self._maybe_display_output(result)
297 self._maybe_display_output(result)
298 return False
298 return False
299
299
300
300
301 __doc__ = __doc__.replace('@AUTOPX_DOC@',
301 __doc__ = __doc__.replace('@AUTOPX_DOC@',
302 " " + ParallelMagics.autopx.__doc__)
302 " " + ParallelMagics.autopx.__doc__)
303 __doc__ = __doc__.replace('@PX_DOC@',
303 __doc__ = __doc__.replace('@PX_DOC@',
304 " " + ParallelMagics.px.__doc__)
304 " " + ParallelMagics.px.__doc__)
305 __doc__ = __doc__.replace('@RESULT_DOC@',
305 __doc__ = __doc__.replace('@RESULT_DOC@',
306 " " + ParallelMagics.result.__doc__)
306 " " + ParallelMagics.result.__doc__)
307
307
308 _loaded = False
308 _loaded = False
309
309
310
310
311 def load_ipython_extension(ip):
311 def load_ipython_extension(ip):
312 """Load the extension in IPython."""
312 """Load the extension in IPython."""
313 global _loaded
313 global _loaded
314 if not _loaded:
314 if not _loaded:
315 ip.register_magics(ParallelMagics)
315 ip.register_magics(ParallelMagics)
316 _loaded = True
316 _loaded = True
@@ -1,218 +1,218 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """
2 """
3 %store magic for lightweight persistence.
3 %store magic for lightweight persistence.
4
4
5 Stores variables, aliases and macros in IPython's database.
5 Stores variables, aliases and macros in IPython's database.
6
6
7 To automatically restore stored variables at startup, add this to your
7 To automatically restore stored variables at startup, add this to your
8 :file:`ipython_config.py` file::
8 :file:`ipython_config.py` file::
9
9
10 c.StoreMagic.autorestore = True
10 c.StoreMagic.autorestore = True
11 """
11 """
12
12
13 import inspect, os, sys, textwrap
13 import inspect, os, sys, textwrap
14
14
15 from IPython.core.error import UsageError
15 from IPython.core.error import UsageError
16 from IPython.core.fakemodule import FakeModule
16 from IPython.core.fakemodule import FakeModule
17 from IPython.core.magic import Magics, register_magics, line_magic
17 from IPython.core.magic import Magics, magics_class, line_magic
18 from IPython.core.plugin import Plugin
18 from IPython.core.plugin import Plugin
19 from IPython.testing.skipdoctest import skip_doctest
19 from IPython.testing.skipdoctest import skip_doctest
20 from IPython.utils.traitlets import Bool, Instance
20 from IPython.utils.traitlets import Bool, Instance
21
21
22
22
23 def restore_aliases(ip):
23 def restore_aliases(ip):
24 staliases = ip.db.get('stored_aliases', {})
24 staliases = ip.db.get('stored_aliases', {})
25 for k,v in staliases.items():
25 for k,v in staliases.items():
26 #print "restore alias",k,v # dbg
26 #print "restore alias",k,v # dbg
27 #self.alias_table[k] = v
27 #self.alias_table[k] = v
28 ip.alias_manager.define_alias(k,v)
28 ip.alias_manager.define_alias(k,v)
29
29
30
30
31 def refresh_variables(ip):
31 def refresh_variables(ip):
32 db = ip.db
32 db = ip.db
33 for key in db.keys('autorestore/*'):
33 for key in db.keys('autorestore/*'):
34 # strip autorestore
34 # strip autorestore
35 justkey = os.path.basename(key)
35 justkey = os.path.basename(key)
36 try:
36 try:
37 obj = db[key]
37 obj = db[key]
38 except KeyError:
38 except KeyError:
39 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey
39 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey
40 print "The error was:", sys.exc_info()[0]
40 print "The error was:", sys.exc_info()[0]
41 else:
41 else:
42 #print "restored",justkey,"=",obj #dbg
42 #print "restored",justkey,"=",obj #dbg
43 ip.user_ns[justkey] = obj
43 ip.user_ns[justkey] = obj
44
44
45
45
46 def restore_dhist(ip):
46 def restore_dhist(ip):
47 ip.user_ns['_dh'] = ip.db.get('dhist',[])
47 ip.user_ns['_dh'] = ip.db.get('dhist',[])
48
48
49
49
50 def restore_data(ip):
50 def restore_data(ip):
51 refresh_variables(ip)
51 refresh_variables(ip)
52 restore_aliases(ip)
52 restore_aliases(ip)
53 restore_dhist(ip)
53 restore_dhist(ip)
54
54
55
55
56 @register_magics
56 @magics_class
57 class StoreMagics(Magics):
57 class StoreMagics(Magics):
58 """Lightweight persistence for python variables.
58 """Lightweight persistence for python variables.
59
59
60 Provides the %store magic."""
60 Provides the %store magic."""
61
61
62 @skip_doctest
62 @skip_doctest
63 @line_magic
63 @line_magic
64 def store(self, parameter_s=''):
64 def store(self, parameter_s=''):
65 """Lightweight persistence for python variables.
65 """Lightweight persistence for python variables.
66
66
67 Example::
67 Example::
68
68
69 In [1]: l = ['hello',10,'world']
69 In [1]: l = ['hello',10,'world']
70 In [2]: %store l
70 In [2]: %store l
71 In [3]: exit
71 In [3]: exit
72
72
73 (IPython session is closed and started again...)
73 (IPython session is closed and started again...)
74
74
75 ville@badger:~$ ipython
75 ville@badger:~$ ipython
76 In [1]: l
76 In [1]: l
77 Out[1]: ['hello', 10, 'world']
77 Out[1]: ['hello', 10, 'world']
78
78
79 Usage:
79 Usage:
80
80
81 * ``%store`` - Show list of all variables and their current
81 * ``%store`` - Show list of all variables and their current
82 values
82 values
83 * ``%store spam`` - Store the *current* value of the variable spam
83 * ``%store spam`` - Store the *current* value of the variable spam
84 to disk
84 to disk
85 * ``%store -d spam`` - Remove the variable and its value from storage
85 * ``%store -d spam`` - Remove the variable and its value from storage
86 * ``%store -z`` - Remove all variables from storage
86 * ``%store -z`` - Remove all variables from storage
87 * ``%store -r`` - Refresh all variables from store (delete
87 * ``%store -r`` - Refresh all variables from store (delete
88 current vals)
88 current vals)
89 * ``%store foo >a.txt`` - Store value of foo to new file a.txt
89 * ``%store foo >a.txt`` - Store value of foo to new file a.txt
90 * ``%store foo >>a.txt`` - Append value of foo to file a.txt
90 * ``%store foo >>a.txt`` - Append value of foo to file a.txt
91
91
92 It should be noted that if you change the value of a variable, you
92 It should be noted that if you change the value of a variable, you
93 need to %store it again if you want to persist the new value.
93 need to %store it again if you want to persist the new value.
94
94
95 Note also that the variables will need to be pickleable; most basic
95 Note also that the variables will need to be pickleable; most basic
96 python types can be safely %store'd.
96 python types can be safely %store'd.
97
97
98 Also aliases can be %store'd across sessions.
98 Also aliases can be %store'd across sessions.
99 """
99 """
100
100
101 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
101 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
102 args = argsl.split(None,1)
102 args = argsl.split(None,1)
103 ip = self.shell
103 ip = self.shell
104 db = ip.db
104 db = ip.db
105 # delete
105 # delete
106 if opts.has_key('d'):
106 if opts.has_key('d'):
107 try:
107 try:
108 todel = args[0]
108 todel = args[0]
109 except IndexError:
109 except IndexError:
110 raise UsageError('You must provide the variable to forget')
110 raise UsageError('You must provide the variable to forget')
111 else:
111 else:
112 try:
112 try:
113 del db['autorestore/' + todel]
113 del db['autorestore/' + todel]
114 except:
114 except:
115 raise UsageError("Can't delete variable '%s'" % todel)
115 raise UsageError("Can't delete variable '%s'" % todel)
116 # reset
116 # reset
117 elif opts.has_key('z'):
117 elif opts.has_key('z'):
118 for k in db.keys('autorestore/*'):
118 for k in db.keys('autorestore/*'):
119 del db[k]
119 del db[k]
120
120
121 elif opts.has_key('r'):
121 elif opts.has_key('r'):
122 refresh_variables(ip)
122 refresh_variables(ip)
123
123
124
124
125 # run without arguments -> list variables & values
125 # run without arguments -> list variables & values
126 elif not args:
126 elif not args:
127 vars = self.db.keys('autorestore/*')
127 vars = self.db.keys('autorestore/*')
128 vars.sort()
128 vars.sort()
129 if vars:
129 if vars:
130 size = max(map(len,vars))
130 size = max(map(len,vars))
131 else:
131 else:
132 size = 0
132 size = 0
133
133
134 print 'Stored variables and their in-db values:'
134 print 'Stored variables and their in-db values:'
135 fmt = '%-'+str(size)+'s -> %s'
135 fmt = '%-'+str(size)+'s -> %s'
136 get = db.get
136 get = db.get
137 for var in vars:
137 for var in vars:
138 justkey = os.path.basename(var)
138 justkey = os.path.basename(var)
139 # print 30 first characters from every var
139 # print 30 first characters from every var
140 print fmt % (justkey,repr(get(var,'<unavailable>'))[:50])
140 print fmt % (justkey,repr(get(var,'<unavailable>'))[:50])
141
141
142 # default action - store the variable
142 # default action - store the variable
143 else:
143 else:
144 # %store foo >file.txt or >>file.txt
144 # %store foo >file.txt or >>file.txt
145 if len(args) > 1 and args[1].startswith('>'):
145 if len(args) > 1 and args[1].startswith('>'):
146 fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
146 fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
147 if args[1].startswith('>>'):
147 if args[1].startswith('>>'):
148 fil = open(fnam,'a')
148 fil = open(fnam,'a')
149 else:
149 else:
150 fil = open(fnam,'w')
150 fil = open(fnam,'w')
151 obj = ip.ev(args[0])
151 obj = ip.ev(args[0])
152 print "Writing '%s' (%s) to file '%s'." % (args[0],
152 print "Writing '%s' (%s) to file '%s'." % (args[0],
153 obj.__class__.__name__, fnam)
153 obj.__class__.__name__, fnam)
154
154
155
155
156 if not isinstance (obj,basestring):
156 if not isinstance (obj,basestring):
157 from pprint import pprint
157 from pprint import pprint
158 pprint(obj,fil)
158 pprint(obj,fil)
159 else:
159 else:
160 fil.write(obj)
160 fil.write(obj)
161 if not obj.endswith('\n'):
161 if not obj.endswith('\n'):
162 fil.write('\n')
162 fil.write('\n')
163
163
164 fil.close()
164 fil.close()
165 return
165 return
166
166
167 # %store foo
167 # %store foo
168 try:
168 try:
169 obj = ip.user_ns[args[0]]
169 obj = ip.user_ns[args[0]]
170 except KeyError:
170 except KeyError:
171 # it might be an alias
171 # it might be an alias
172 # This needs to be refactored to use the new AliasManager stuff.
172 # This needs to be refactored to use the new AliasManager stuff.
173 if args[0] in self.alias_manager:
173 if args[0] in self.alias_manager:
174 name = args[0]
174 name = args[0]
175 nargs, cmd = self.alias_manager.alias_table[ name ]
175 nargs, cmd = self.alias_manager.alias_table[ name ]
176 staliases = db.get('stored_aliases',{})
176 staliases = db.get('stored_aliases',{})
177 staliases[ name ] = cmd
177 staliases[ name ] = cmd
178 db['stored_aliases'] = staliases
178 db['stored_aliases'] = staliases
179 print "Alias stored: %s (%s)" % (name, cmd)
179 print "Alias stored: %s (%s)" % (name, cmd)
180 return
180 return
181 else:
181 else:
182 raise UsageError("Unknown variable '%s'" % args[0])
182 raise UsageError("Unknown variable '%s'" % args[0])
183
183
184 else:
184 else:
185 if isinstance(inspect.getmodule(obj), FakeModule):
185 if isinstance(inspect.getmodule(obj), FakeModule):
186 print textwrap.dedent("""\
186 print textwrap.dedent("""\
187 Warning:%s is %s
187 Warning:%s is %s
188 Proper storage of interactively declared classes (or instances
188 Proper storage of interactively declared classes (or instances
189 of those classes) is not possible! Only instances
189 of those classes) is not possible! Only instances
190 of classes in real modules on file system can be %%store'd.
190 of classes in real modules on file system can be %%store'd.
191 """ % (args[0], obj) )
191 """ % (args[0], obj) )
192 return
192 return
193 #pickled = pickle.dumps(obj)
193 #pickled = pickle.dumps(obj)
194 self.db[ 'autorestore/' + args[0] ] = obj
194 self.db[ 'autorestore/' + args[0] ] = obj
195 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
195 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
196
196
197
197
198 class StoreMagic(Plugin):
198 class StoreMagic(Plugin):
199 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
199 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
200 autorestore = Bool(False, config=True)
200 autorestore = Bool(False, config=True)
201
201
202 def __init__(self, shell, config):
202 def __init__(self, shell, config):
203 super(StoreMagic, self).__init__(shell=shell, config=config)
203 super(StoreMagic, self).__init__(shell=shell, config=config)
204 shell.register_magics(StoreMagics)
204 shell.register_magics(StoreMagics)
205
205
206 if self.autorestore:
206 if self.autorestore:
207 restore_data(shell)
207 restore_data(shell)
208
208
209
209
210 _loaded = False
210 _loaded = False
211
211
212 def load_ipython_extension(ip):
212 def load_ipython_extension(ip):
213 """Load the extension in IPython."""
213 """Load the extension in IPython."""
214 global _loaded
214 global _loaded
215 if not _loaded:
215 if not _loaded:
216 plugin = StoreMagic(shell=ip, config=ip.config)
216 plugin = StoreMagic(shell=ip, config=ip.config)
217 ip.plugin_manager.register_plugin('storemagic', plugin)
217 ip.plugin_manager.register_plugin('storemagic', plugin)
218 _loaded = True
218 _loaded = True
@@ -1,277 +1,277 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """
2 """
3 An embedded IPython shell.
3 An embedded IPython shell.
4
4
5 Authors:
5 Authors:
6
6
7 * Brian Granger
7 * Brian Granger
8 * Fernando Perez
8 * Fernando Perez
9
9
10 Notes
10 Notes
11 -----
11 -----
12 """
12 """
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Copyright (C) 2008-2011 The IPython Development Team
15 # Copyright (C) 2008-2011 The IPython Development Team
16 #
16 #
17 # Distributed under the terms of the BSD License. The full license is in
17 # Distributed under the terms of the BSD License. The full license is in
18 # the file COPYING, distributed as part of this software.
18 # the file COPYING, distributed as part of this software.
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20
20
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 # Imports
22 # Imports
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 from __future__ import with_statement
25 from __future__ import with_statement
26
26
27 import sys
27 import sys
28 from contextlib import nested
28 from contextlib import nested
29 import warnings
29 import warnings
30
30
31 from IPython.core import ultratb
31 from IPython.core import ultratb
32 from IPython.core.magic import Magics, register_magics, line_magic
32 from IPython.core.magic import Magics, magics_class, line_magic
33 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
33 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
34 from IPython.frontend.terminal.ipapp import load_default_config
34 from IPython.frontend.terminal.ipapp import load_default_config
35
35
36 from IPython.utils.traitlets import Bool, CBool, Unicode
36 from IPython.utils.traitlets import Bool, CBool, Unicode
37 from IPython.utils.io import ask_yes_no
37 from IPython.utils.io import ask_yes_no
38
38
39
39
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41 # Classes and functions
41 # Classes and functions
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43
43
44 # This is an additional magic that is exposed in embedded shells.
44 # This is an additional magic that is exposed in embedded shells.
45 @register_magics
45 @magics_class
46 class EmbeddedMagics(Magics):
46 class EmbeddedMagics(Magics):
47
47
48 @line_magic
48 @line_magic
49 def kill_embedded(self, parameter_s=''):
49 def kill_embedded(self, parameter_s=''):
50 """%kill_embedded : deactivate for good the current embedded IPython.
50 """%kill_embedded : deactivate for good the current embedded IPython.
51
51
52 This function (after asking for confirmation) sets an internal flag so
52 This function (after asking for confirmation) sets an internal flag so
53 that an embedded IPython will never activate again. This is useful to
53 that an embedded IPython will never activate again. This is useful to
54 permanently disable a shell that is being called inside a loop: once
54 permanently disable a shell that is being called inside a loop: once
55 you've figured out what you needed from it, you may then kill it and
55 you've figured out what you needed from it, you may then kill it and
56 the program will then continue to run without the interactive shell
56 the program will then continue to run without the interactive shell
57 interfering again.
57 interfering again.
58 """
58 """
59
59
60 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
60 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
61 "(y/n)? [y/N] ",'n')
61 "(y/n)? [y/N] ",'n')
62 if kill:
62 if kill:
63 self.shell.embedded_active = False
63 self.shell.embedded_active = False
64 print ("This embedded IPython will not reactivate anymore "
64 print ("This embedded IPython will not reactivate anymore "
65 "once you exit.")
65 "once you exit.")
66
66
67
67
68 class InteractiveShellEmbed(TerminalInteractiveShell):
68 class InteractiveShellEmbed(TerminalInteractiveShell):
69
69
70 dummy_mode = Bool(False)
70 dummy_mode = Bool(False)
71 exit_msg = Unicode('')
71 exit_msg = Unicode('')
72 embedded = CBool(True)
72 embedded = CBool(True)
73 embedded_active = CBool(True)
73 embedded_active = CBool(True)
74 # Like the base class display_banner is not configurable, but here it
74 # Like the base class display_banner is not configurable, but here it
75 # is True by default.
75 # is True by default.
76 display_banner = CBool(True)
76 display_banner = CBool(True)
77
77
78 def __init__(self, config=None, ipython_dir=None, user_ns=None,
78 def __init__(self, config=None, ipython_dir=None, user_ns=None,
79 user_module=None, custom_exceptions=((),None),
79 user_module=None, custom_exceptions=((),None),
80 usage=None, banner1=None, banner2=None,
80 usage=None, banner1=None, banner2=None,
81 display_banner=None, exit_msg=u'', user_global_ns=None):
81 display_banner=None, exit_msg=u'', user_global_ns=None):
82
82
83 if user_global_ns is not None:
83 if user_global_ns is not None:
84 warnings.warn("user_global_ns has been replaced by user_module. The\
84 warnings.warn("user_global_ns has been replaced by user_module. The\
85 parameter will be ignored.", DeprecationWarning)
85 parameter will be ignored.", DeprecationWarning)
86
86
87 super(InteractiveShellEmbed,self).__init__(
87 super(InteractiveShellEmbed,self).__init__(
88 config=config, ipython_dir=ipython_dir, user_ns=user_ns,
88 config=config, ipython_dir=ipython_dir, user_ns=user_ns,
89 user_module=user_module, custom_exceptions=custom_exceptions,
89 user_module=user_module, custom_exceptions=custom_exceptions,
90 usage=usage, banner1=banner1, banner2=banner2,
90 usage=usage, banner1=banner1, banner2=banner2,
91 display_banner=display_banner
91 display_banner=display_banner
92 )
92 )
93
93
94 self.exit_msg = exit_msg
94 self.exit_msg = exit_msg
95
95
96 # don't use the ipython crash handler so that user exceptions aren't
96 # don't use the ipython crash handler so that user exceptions aren't
97 # trapped
97 # trapped
98 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
98 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
99 mode=self.xmode,
99 mode=self.xmode,
100 call_pdb=self.pdb)
100 call_pdb=self.pdb)
101
101
102 def init_sys_modules(self):
102 def init_sys_modules(self):
103 pass
103 pass
104
104
105 def init_magics(self):
105 def init_magics(self):
106 super(InteractiveShellEmbed, self).init_magics()
106 super(InteractiveShellEmbed, self).init_magics()
107 self.register_magics(EmbeddedMagics)
107 self.register_magics(EmbeddedMagics)
108
108
109 def __call__(self, header='', local_ns=None, module=None, dummy=None,
109 def __call__(self, header='', local_ns=None, module=None, dummy=None,
110 stack_depth=1, global_ns=None):
110 stack_depth=1, global_ns=None):
111 """Activate the interactive interpreter.
111 """Activate the interactive interpreter.
112
112
113 __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start
113 __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start
114 the interpreter shell with the given local and global namespaces, and
114 the interpreter shell with the given local and global namespaces, and
115 optionally print a header string at startup.
115 optionally print a header string at startup.
116
116
117 The shell can be globally activated/deactivated using the
117 The shell can be globally activated/deactivated using the
118 dummy_mode attribute. This allows you to turn off a shell used
118 dummy_mode attribute. This allows you to turn off a shell used
119 for debugging globally.
119 for debugging globally.
120
120
121 However, *each* time you call the shell you can override the current
121 However, *each* time you call the shell you can override the current
122 state of dummy_mode with the optional keyword parameter 'dummy'. For
122 state of dummy_mode with the optional keyword parameter 'dummy'. For
123 example, if you set dummy mode on with IPShell.dummy_mode = True, you
123 example, if you set dummy mode on with IPShell.dummy_mode = True, you
124 can still have a specific call work by making it as IPShell(dummy=False).
124 can still have a specific call work by making it as IPShell(dummy=False).
125 """
125 """
126
126
127 # If the user has turned it off, go away
127 # If the user has turned it off, go away
128 if not self.embedded_active:
128 if not self.embedded_active:
129 return
129 return
130
130
131 # Normal exits from interactive mode set this flag, so the shell can't
131 # Normal exits from interactive mode set this flag, so the shell can't
132 # re-enter (it checks this variable at the start of interactive mode).
132 # re-enter (it checks this variable at the start of interactive mode).
133 self.exit_now = False
133 self.exit_now = False
134
134
135 # Allow the dummy parameter to override the global __dummy_mode
135 # Allow the dummy parameter to override the global __dummy_mode
136 if dummy or (dummy != 0 and self.dummy_mode):
136 if dummy or (dummy != 0 and self.dummy_mode):
137 return
137 return
138
138
139 if self.has_readline:
139 if self.has_readline:
140 self.set_readline_completer()
140 self.set_readline_completer()
141
141
142 # self.banner is auto computed
142 # self.banner is auto computed
143 if header:
143 if header:
144 self.old_banner2 = self.banner2
144 self.old_banner2 = self.banner2
145 self.banner2 = self.banner2 + '\n' + header + '\n'
145 self.banner2 = self.banner2 + '\n' + header + '\n'
146 else:
146 else:
147 self.old_banner2 = ''
147 self.old_banner2 = ''
148
148
149 # Call the embedding code with a stack depth of 1 so it can skip over
149 # Call the embedding code with a stack depth of 1 so it can skip over
150 # our call and get the original caller's namespaces.
150 # our call and get the original caller's namespaces.
151 self.mainloop(local_ns, module, stack_depth=stack_depth, global_ns=global_ns)
151 self.mainloop(local_ns, module, stack_depth=stack_depth, global_ns=global_ns)
152
152
153 self.banner2 = self.old_banner2
153 self.banner2 = self.old_banner2
154
154
155 if self.exit_msg is not None:
155 if self.exit_msg is not None:
156 print self.exit_msg
156 print self.exit_msg
157
157
158 def mainloop(self, local_ns=None, module=None, stack_depth=0,
158 def mainloop(self, local_ns=None, module=None, stack_depth=0,
159 display_banner=None, global_ns=None):
159 display_banner=None, global_ns=None):
160 """Embeds IPython into a running python program.
160 """Embeds IPython into a running python program.
161
161
162 Input:
162 Input:
163
163
164 - header: An optional header message can be specified.
164 - header: An optional header message can be specified.
165
165
166 - local_ns, module: working local namespace (a dict) and module (a
166 - local_ns, module: working local namespace (a dict) and module (a
167 module or similar object). If given as None, they are automatically
167 module or similar object). If given as None, they are automatically
168 taken from the scope where the shell was called, so that
168 taken from the scope where the shell was called, so that
169 program variables become visible.
169 program variables become visible.
170
170
171 - stack_depth: specifies how many levels in the stack to go to
171 - stack_depth: specifies how many levels in the stack to go to
172 looking for namespaces (when local_ns or module is None). This
172 looking for namespaces (when local_ns or module is None). This
173 allows an intermediate caller to make sure that this function gets
173 allows an intermediate caller to make sure that this function gets
174 the namespace from the intended level in the stack. By default (0)
174 the namespace from the intended level in the stack. By default (0)
175 it will get its locals and globals from the immediate caller.
175 it will get its locals and globals from the immediate caller.
176
176
177 Warning: it's possible to use this in a program which is being run by
177 Warning: it's possible to use this in a program which is being run by
178 IPython itself (via %run), but some funny things will happen (a few
178 IPython itself (via %run), but some funny things will happen (a few
179 globals get overwritten). In the future this will be cleaned up, as
179 globals get overwritten). In the future this will be cleaned up, as
180 there is no fundamental reason why it can't work perfectly."""
180 there is no fundamental reason why it can't work perfectly."""
181
181
182 if (global_ns is not None) and (module is None):
182 if (global_ns is not None) and (module is None):
183 class DummyMod(object):
183 class DummyMod(object):
184 """A dummy module object for embedded IPython."""
184 """A dummy module object for embedded IPython."""
185 pass
185 pass
186 warnings.warn("global_ns is deprecated, use module instead.", DeprecationWarning)
186 warnings.warn("global_ns is deprecated, use module instead.", DeprecationWarning)
187 module = DummyMod()
187 module = DummyMod()
188 module.__dict__ = global_ns
188 module.__dict__ = global_ns
189
189
190 # Get locals and globals from caller
190 # Get locals and globals from caller
191 if (local_ns is None or module is None) and self.default_user_namespaces:
191 if (local_ns is None or module is None) and self.default_user_namespaces:
192 call_frame = sys._getframe(stack_depth).f_back
192 call_frame = sys._getframe(stack_depth).f_back
193
193
194 if local_ns is None:
194 if local_ns is None:
195 local_ns = call_frame.f_locals
195 local_ns = call_frame.f_locals
196 if module is None:
196 if module is None:
197 global_ns = call_frame.f_globals
197 global_ns = call_frame.f_globals
198 module = sys.modules[global_ns['__name__']]
198 module = sys.modules[global_ns['__name__']]
199
199
200 # Save original namespace and module so we can restore them after
200 # Save original namespace and module so we can restore them after
201 # embedding; otherwise the shell doesn't shut down correctly.
201 # embedding; otherwise the shell doesn't shut down correctly.
202 orig_user_module = self.user_module
202 orig_user_module = self.user_module
203 orig_user_ns = self.user_ns
203 orig_user_ns = self.user_ns
204
204
205 # Update namespaces and fire up interpreter
205 # Update namespaces and fire up interpreter
206
206
207 # The global one is easy, we can just throw it in
207 # The global one is easy, we can just throw it in
208 if module is not None:
208 if module is not None:
209 self.user_module = module
209 self.user_module = module
210
210
211 # But the user/local one is tricky: ipython needs it to store internal
211 # But the user/local one is tricky: ipython needs it to store internal
212 # data, but we also need the locals. We'll throw our hidden variables
212 # data, but we also need the locals. We'll throw our hidden variables
213 # like _ih and get_ipython() into the local namespace, but delete them
213 # like _ih and get_ipython() into the local namespace, but delete them
214 # later.
214 # later.
215 if local_ns is not None:
215 if local_ns is not None:
216 self.user_ns = local_ns
216 self.user_ns = local_ns
217 self.init_user_ns()
217 self.init_user_ns()
218
218
219 # Patch for global embedding to make sure that things don't overwrite
219 # Patch for global embedding to make sure that things don't overwrite
220 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
220 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
221 # FIXME. Test this a bit more carefully (the if.. is new)
221 # FIXME. Test this a bit more carefully (the if.. is new)
222 # N.B. This can't now ever be called. Not sure what it was for.
222 # N.B. This can't now ever be called. Not sure what it was for.
223 # And now, since it wasn't called in the previous version, I'm
223 # And now, since it wasn't called in the previous version, I'm
224 # commenting out these lines so they can't be called with my new changes
224 # commenting out these lines so they can't be called with my new changes
225 # --TK, 2011-12-10
225 # --TK, 2011-12-10
226 #if local_ns is None and module is None:
226 #if local_ns is None and module is None:
227 # self.user_global_ns.update(__main__.__dict__)
227 # self.user_global_ns.update(__main__.__dict__)
228
228
229 # make sure the tab-completer has the correct frame information, so it
229 # make sure the tab-completer has the correct frame information, so it
230 # actually completes using the frame's locals/globals
230 # actually completes using the frame's locals/globals
231 self.set_completer_frame()
231 self.set_completer_frame()
232
232
233 with nested(self.builtin_trap, self.display_trap):
233 with nested(self.builtin_trap, self.display_trap):
234 self.interact(display_banner=display_banner)
234 self.interact(display_banner=display_banner)
235
235
236 # now, purge out the local namespace of IPython's hidden variables.
236 # now, purge out the local namespace of IPython's hidden variables.
237 if local_ns is not None:
237 if local_ns is not None:
238 for name in self.user_ns_hidden:
238 for name in self.user_ns_hidden:
239 local_ns.pop(name, None)
239 local_ns.pop(name, None)
240
240
241 # Restore original namespace so shell can shut down when we exit.
241 # Restore original namespace so shell can shut down when we exit.
242 self.user_module = orig_user_module
242 self.user_module = orig_user_module
243 self.user_ns = orig_user_ns
243 self.user_ns = orig_user_ns
244
244
245 _embedded_shell = None
245 _embedded_shell = None
246
246
247
247
248 def embed(**kwargs):
248 def embed(**kwargs):
249 """Call this to embed IPython at the current point in your program.
249 """Call this to embed IPython at the current point in your program.
250
250
251 The first invocation of this will create an :class:`InteractiveShellEmbed`
251 The first invocation of this will create an :class:`InteractiveShellEmbed`
252 instance and then call it. Consecutive calls just call the already
252 instance and then call it. Consecutive calls just call the already
253 created instance.
253 created instance.
254
254
255 Here is a simple example::
255 Here is a simple example::
256
256
257 from IPython import embed
257 from IPython import embed
258 a = 10
258 a = 10
259 b = 20
259 b = 20
260 embed('First time')
260 embed('First time')
261 c = 30
261 c = 30
262 d = 40
262 d = 40
263 embed
263 embed
264
264
265 Full customization can be done by passing a :class:`Struct` in as the
265 Full customization can be done by passing a :class:`Struct` in as the
266 config argument.
266 config argument.
267 """
267 """
268 config = kwargs.get('config')
268 config = kwargs.get('config')
269 header = kwargs.pop('header', u'')
269 header = kwargs.pop('header', u'')
270 if config is None:
270 if config is None:
271 config = load_default_config()
271 config = load_default_config()
272 config.InteractiveShellEmbed = config.TerminalInteractiveShell
272 config.InteractiveShellEmbed = config.TerminalInteractiveShell
273 kwargs['config'] = config
273 kwargs['config'] = config
274 global _embedded_shell
274 global _embedded_shell
275 if _embedded_shell is None:
275 if _embedded_shell is None:
276 _embedded_shell = InteractiveShellEmbed(**kwargs)
276 _embedded_shell = InteractiveShellEmbed(**kwargs)
277 _embedded_shell(header=header, stack_depth=2)
277 _embedded_shell(header=header, stack_depth=2)
@@ -1,676 +1,676 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Subclass of InteractiveShell for terminal based frontends."""
2 """Subclass of InteractiveShell for terminal based frontends."""
3
3
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 # Copyright (C) 2008-2011 The IPython Development Team
7 # Copyright (C) 2008-2011 The IPython Development Team
8 #
8 #
9 # Distributed under the terms of the BSD License. The full license is in
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import bdb
17 import bdb
18 import os
18 import os
19 import re
19 import re
20 import sys
20 import sys
21 import textwrap
21 import textwrap
22
22
23 from contextlib import nested
23 from contextlib import nested
24
24
25 from IPython.core.error import TryNext, UsageError
25 from IPython.core.error import TryNext, UsageError
26 from IPython.core.usage import interactive_usage, default_banner
26 from IPython.core.usage import interactive_usage, default_banner
27 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
27 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
28 from IPython.core.magic import Magics, register_magics, line_magic
28 from IPython.core.magic import Magics, magics_class, line_magic
29 from IPython.testing.skipdoctest import skip_doctest
29 from IPython.testing.skipdoctest import skip_doctest
30 from IPython.utils.encoding import get_stream_enc
30 from IPython.utils.encoding import get_stream_enc
31 from IPython.utils import py3compat
31 from IPython.utils import py3compat
32 from IPython.utils.terminal import toggle_set_term_title, set_term_title
32 from IPython.utils.terminal import toggle_set_term_title, set_term_title
33 from IPython.utils.process import abbrev_cwd
33 from IPython.utils.process import abbrev_cwd
34 from IPython.utils.warn import warn, error
34 from IPython.utils.warn import warn, error
35 from IPython.utils.text import num_ini_spaces, SList
35 from IPython.utils.text import num_ini_spaces, SList
36 from IPython.utils.traitlets import Integer, CBool, Unicode
36 from IPython.utils.traitlets import Integer, CBool, Unicode
37
37
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39 # Utilities
39 # Utilities
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41
41
42 def get_default_editor():
42 def get_default_editor():
43 try:
43 try:
44 ed = os.environ['EDITOR']
44 ed = os.environ['EDITOR']
45 except KeyError:
45 except KeyError:
46 if os.name == 'posix':
46 if os.name == 'posix':
47 ed = 'vi' # the only one guaranteed to be there!
47 ed = 'vi' # the only one guaranteed to be there!
48 else:
48 else:
49 ed = 'notepad' # same in Windows!
49 ed = 'notepad' # same in Windows!
50 return ed
50 return ed
51
51
52
52
53 def get_pasted_lines(sentinel, l_input=py3compat.input):
53 def get_pasted_lines(sentinel, l_input=py3compat.input):
54 """ Yield pasted lines until the user enters the given sentinel value.
54 """ Yield pasted lines until the user enters the given sentinel value.
55 """
55 """
56 print "Pasting code; enter '%s' alone on the line to stop or use Ctrl-D." \
56 print "Pasting code; enter '%s' alone on the line to stop or use Ctrl-D." \
57 % sentinel
57 % sentinel
58 while True:
58 while True:
59 try:
59 try:
60 l = l_input(':')
60 l = l_input(':')
61 if l == sentinel:
61 if l == sentinel:
62 return
62 return
63 else:
63 else:
64 yield l
64 yield l
65 except EOFError:
65 except EOFError:
66 print '<EOF>'
66 print '<EOF>'
67 return
67 return
68
68
69
69
70 def strip_email_quotes(raw_lines):
70 def strip_email_quotes(raw_lines):
71 """ Strip email quotation marks at the beginning of each line.
71 """ Strip email quotation marks at the beginning of each line.
72
72
73 We don't do any more input transofrmations here because the main shell's
73 We don't do any more input transofrmations here because the main shell's
74 prefiltering handles other cases.
74 prefiltering handles other cases.
75 """
75 """
76 lines = [re.sub(r'^\s*(\s?>)+', '', l) for l in raw_lines]
76 lines = [re.sub(r'^\s*(\s?>)+', '', l) for l in raw_lines]
77 return '\n'.join(lines) + '\n'
77 return '\n'.join(lines) + '\n'
78
78
79
79
80 # These two functions are needed by the %paste/%cpaste magics. In practice
80 # These two functions are needed by the %paste/%cpaste magics. In practice
81 # they are basically methods (they take the shell as their first argument), but
81 # they are basically methods (they take the shell as their first argument), but
82 # we leave them as standalone functions because eventually the magics
82 # we leave them as standalone functions because eventually the magics
83 # themselves will become separate objects altogether. At that point, the
83 # themselves will become separate objects altogether. At that point, the
84 # magics will have access to the shell object, and these functions can be made
84 # magics will have access to the shell object, and these functions can be made
85 # methods of the magic object, but not of the shell.
85 # methods of the magic object, but not of the shell.
86
86
87 def store_or_execute(shell, block, name):
87 def store_or_execute(shell, block, name):
88 """ Execute a block, or store it in a variable, per the user's request.
88 """ Execute a block, or store it in a variable, per the user's request.
89 """
89 """
90 # Dedent and prefilter so what we store matches what is executed by
90 # Dedent and prefilter so what we store matches what is executed by
91 # run_cell.
91 # run_cell.
92 b = shell.prefilter(textwrap.dedent(block))
92 b = shell.prefilter(textwrap.dedent(block))
93
93
94 if name:
94 if name:
95 # If storing it for further editing, run the prefilter on it
95 # If storing it for further editing, run the prefilter on it
96 shell.user_ns[name] = SList(b.splitlines())
96 shell.user_ns[name] = SList(b.splitlines())
97 print "Block assigned to '%s'" % name
97 print "Block assigned to '%s'" % name
98 else:
98 else:
99 shell.user_ns['pasted_block'] = b
99 shell.user_ns['pasted_block'] = b
100 shell.run_cell(b)
100 shell.run_cell(b)
101
101
102
102
103 def rerun_pasted(shell, name='pasted_block'):
103 def rerun_pasted(shell, name='pasted_block'):
104 """ Rerun a previously pasted command.
104 """ Rerun a previously pasted command.
105 """
105 """
106 b = shell.user_ns.get(name)
106 b = shell.user_ns.get(name)
107
107
108 # Sanity checks
108 # Sanity checks
109 if b is None:
109 if b is None:
110 raise UsageError('No previous pasted block available')
110 raise UsageError('No previous pasted block available')
111 if not isinstance(b, basestring):
111 if not isinstance(b, basestring):
112 raise UsageError(
112 raise UsageError(
113 "Variable 'pasted_block' is not a string, can't execute")
113 "Variable 'pasted_block' is not a string, can't execute")
114
114
115 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
115 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
116 shell.run_cell(b)
116 shell.run_cell(b)
117
117
118
118
119 #------------------------------------------------------------------------
119 #------------------------------------------------------------------------
120 # Terminal-specific magics
120 # Terminal-specific magics
121 #------------------------------------------------------------------------
121 #------------------------------------------------------------------------
122
122
123 @register_magics
123 @magics_class
124 class TerminalMagics(Magics):
124 class TerminalMagics(Magics):
125
125
126 @line_magic
126 @line_magic
127 def autoindent(self, parameter_s = ''):
127 def autoindent(self, parameter_s = ''):
128 """Toggle autoindent on/off (if available)."""
128 """Toggle autoindent on/off (if available)."""
129
129
130 self.shell.set_autoindent()
130 self.shell.set_autoindent()
131 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
131 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
132
132
133 @skip_doctest
133 @skip_doctest
134 @line_magic
134 @line_magic
135 def cpaste(self, parameter_s=''):
135 def cpaste(self, parameter_s=''):
136 """Paste & execute a pre-formatted code block from clipboard.
136 """Paste & execute a pre-formatted code block from clipboard.
137
137
138 You must terminate the block with '--' (two minus-signs) or Ctrl-D
138 You must terminate the block with '--' (two minus-signs) or Ctrl-D
139 alone on the line. You can also provide your own sentinel with '%paste
139 alone on the line. You can also provide your own sentinel with '%paste
140 -s %%' ('%%' is the new sentinel for this operation)
140 -s %%' ('%%' is the new sentinel for this operation)
141
141
142 The block is dedented prior to execution to enable execution of method
142 The block is dedented prior to execution to enable execution of method
143 definitions. '>' and '+' characters at the beginning of a line are
143 definitions. '>' and '+' characters at the beginning of a line are
144 ignored, to allow pasting directly from e-mails, diff files and
144 ignored, to allow pasting directly from e-mails, diff files and
145 doctests (the '...' continuation prompt is also stripped). The
145 doctests (the '...' continuation prompt is also stripped). The
146 executed block is also assigned to variable named 'pasted_block' for
146 executed block is also assigned to variable named 'pasted_block' for
147 later editing with '%edit pasted_block'.
147 later editing with '%edit pasted_block'.
148
148
149 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
149 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
150 This assigns the pasted block to variable 'foo' as string, without
150 This assigns the pasted block to variable 'foo' as string, without
151 dedenting or executing it (preceding >>> and + is still stripped)
151 dedenting or executing it (preceding >>> and + is still stripped)
152
152
153 '%cpaste -r' re-executes the block previously entered by cpaste.
153 '%cpaste -r' re-executes the block previously entered by cpaste.
154
154
155 Do not be alarmed by garbled output on Windows (it's a readline bug).
155 Do not be alarmed by garbled output on Windows (it's a readline bug).
156 Just press enter and type -- (and press enter again) and the block
156 Just press enter and type -- (and press enter again) and the block
157 will be what was just pasted.
157 will be what was just pasted.
158
158
159 IPython statements (magics, shell escapes) are not supported (yet).
159 IPython statements (magics, shell escapes) are not supported (yet).
160
160
161 See also
161 See also
162 --------
162 --------
163 paste: automatically pull code from clipboard.
163 paste: automatically pull code from clipboard.
164
164
165 Examples
165 Examples
166 --------
166 --------
167 ::
167 ::
168
168
169 In [8]: %cpaste
169 In [8]: %cpaste
170 Pasting code; enter '--' alone on the line to stop.
170 Pasting code; enter '--' alone on the line to stop.
171 :>>> a = ["world!", "Hello"]
171 :>>> a = ["world!", "Hello"]
172 :>>> print " ".join(sorted(a))
172 :>>> print " ".join(sorted(a))
173 :--
173 :--
174 Hello world!
174 Hello world!
175 """
175 """
176
176
177 opts, name = self.parse_options(parameter_s, 'rs:', mode='string')
177 opts, name = self.parse_options(parameter_s, 'rs:', mode='string')
178 if 'r' in opts:
178 if 'r' in opts:
179 rerun_pasted(self.shell)
179 rerun_pasted(self.shell)
180 return
180 return
181
181
182 sentinel = opts.get('s', '--')
182 sentinel = opts.get('s', '--')
183 block = strip_email_quotes(get_pasted_lines(sentinel))
183 block = strip_email_quotes(get_pasted_lines(sentinel))
184 store_or_execute(self.shell, block, name)
184 store_or_execute(self.shell, block, name)
185
185
186 @line_magic
186 @line_magic
187 def paste(self, parameter_s=''):
187 def paste(self, parameter_s=''):
188 """Paste & execute a pre-formatted code block from clipboard.
188 """Paste & execute a pre-formatted code block from clipboard.
189
189
190 The text is pulled directly from the clipboard without user
190 The text is pulled directly from the clipboard without user
191 intervention and printed back on the screen before execution (unless
191 intervention and printed back on the screen before execution (unless
192 the -q flag is given to force quiet mode).
192 the -q flag is given to force quiet mode).
193
193
194 The block is dedented prior to execution to enable execution of method
194 The block is dedented prior to execution to enable execution of method
195 definitions. '>' and '+' characters at the beginning of a line are
195 definitions. '>' and '+' characters at the beginning of a line are
196 ignored, to allow pasting directly from e-mails, diff files and
196 ignored, to allow pasting directly from e-mails, diff files and
197 doctests (the '...' continuation prompt is also stripped). The
197 doctests (the '...' continuation prompt is also stripped). The
198 executed block is also assigned to variable named 'pasted_block' for
198 executed block is also assigned to variable named 'pasted_block' for
199 later editing with '%edit pasted_block'.
199 later editing with '%edit pasted_block'.
200
200
201 You can also pass a variable name as an argument, e.g. '%paste foo'.
201 You can also pass a variable name as an argument, e.g. '%paste foo'.
202 This assigns the pasted block to variable 'foo' as string, without
202 This assigns the pasted block to variable 'foo' as string, without
203 dedenting or executing it (preceding >>> and + is still stripped)
203 dedenting or executing it (preceding >>> and + is still stripped)
204
204
205 Options
205 Options
206 -------
206 -------
207
207
208 -r: re-executes the block previously entered by cpaste.
208 -r: re-executes the block previously entered by cpaste.
209
209
210 -q: quiet mode: do not echo the pasted text back to the terminal.
210 -q: quiet mode: do not echo the pasted text back to the terminal.
211
211
212 IPython statements (magics, shell escapes) are not supported (yet).
212 IPython statements (magics, shell escapes) are not supported (yet).
213
213
214 See also
214 See also
215 --------
215 --------
216 cpaste: manually paste code into terminal until you mark its end.
216 cpaste: manually paste code into terminal until you mark its end.
217 """
217 """
218 opts, name = self.parse_options(parameter_s, 'rq', mode='string')
218 opts, name = self.parse_options(parameter_s, 'rq', mode='string')
219 if 'r' in opts:
219 if 'r' in opts:
220 rerun_pasted(self.shell)
220 rerun_pasted(self.shell)
221 return
221 return
222 try:
222 try:
223 text = self.shell.hooks.clipboard_get()
223 text = self.shell.hooks.clipboard_get()
224 block = strip_email_quotes(text.splitlines())
224 block = strip_email_quotes(text.splitlines())
225 except TryNext as clipboard_exc:
225 except TryNext as clipboard_exc:
226 message = getattr(clipboard_exc, 'args')
226 message = getattr(clipboard_exc, 'args')
227 if message:
227 if message:
228 error(message[0])
228 error(message[0])
229 else:
229 else:
230 error('Could not get text from the clipboard.')
230 error('Could not get text from the clipboard.')
231 return
231 return
232
232
233 # By default, echo back to terminal unless quiet mode is requested
233 # By default, echo back to terminal unless quiet mode is requested
234 if 'q' not in opts:
234 if 'q' not in opts:
235 write = self.shell.write
235 write = self.shell.write
236 write(self.shell.pycolorize(block))
236 write(self.shell.pycolorize(block))
237 if not block.endswith('\n'):
237 if not block.endswith('\n'):
238 write('\n')
238 write('\n')
239 write("## -- End pasted text --\n")
239 write("## -- End pasted text --\n")
240
240
241 store_or_execute(self.shell, block, name)
241 store_or_execute(self.shell, block, name)
242
242
243 # Class-level: add a '%cls' magic only on Windows
243 # Class-level: add a '%cls' magic only on Windows
244 if sys.platform == 'win32':
244 if sys.platform == 'win32':
245 @line_magic
245 @line_magic
246 def cls(self, s):
246 def cls(self, s):
247 """Clear screen.
247 """Clear screen.
248 """
248 """
249 os.system("cls")
249 os.system("cls")
250
250
251 #-----------------------------------------------------------------------------
251 #-----------------------------------------------------------------------------
252 # Main class
252 # Main class
253 #-----------------------------------------------------------------------------
253 #-----------------------------------------------------------------------------
254
254
255 class TerminalInteractiveShell(InteractiveShell):
255 class TerminalInteractiveShell(InteractiveShell):
256
256
257 autoedit_syntax = CBool(False, config=True,
257 autoedit_syntax = CBool(False, config=True,
258 help="auto editing of files with syntax errors.")
258 help="auto editing of files with syntax errors.")
259 banner = Unicode('')
259 banner = Unicode('')
260 banner1 = Unicode(default_banner, config=True,
260 banner1 = Unicode(default_banner, config=True,
261 help="""The part of the banner to be printed before the profile"""
261 help="""The part of the banner to be printed before the profile"""
262 )
262 )
263 banner2 = Unicode('', config=True,
263 banner2 = Unicode('', config=True,
264 help="""The part of the banner to be printed after the profile"""
264 help="""The part of the banner to be printed after the profile"""
265 )
265 )
266 confirm_exit = CBool(True, config=True,
266 confirm_exit = CBool(True, config=True,
267 help="""
267 help="""
268 Set to confirm when you try to exit IPython with an EOF (Control-D
268 Set to confirm when you try to exit IPython with an EOF (Control-D
269 in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
269 in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
270 you can force a direct exit without any confirmation.""",
270 you can force a direct exit without any confirmation.""",
271 )
271 )
272 # This display_banner only controls whether or not self.show_banner()
272 # This display_banner only controls whether or not self.show_banner()
273 # is called when mainloop/interact are called. The default is False
273 # is called when mainloop/interact are called. The default is False
274 # because for the terminal based application, the banner behavior
274 # because for the terminal based application, the banner behavior
275 # is controlled by Global.display_banner, which IPythonApp looks at
275 # is controlled by Global.display_banner, which IPythonApp looks at
276 # to determine if *it* should call show_banner() by hand or not.
276 # to determine if *it* should call show_banner() by hand or not.
277 display_banner = CBool(False) # This isn't configurable!
277 display_banner = CBool(False) # This isn't configurable!
278 embedded = CBool(False)
278 embedded = CBool(False)
279 embedded_active = CBool(False)
279 embedded_active = CBool(False)
280 editor = Unicode(get_default_editor(), config=True,
280 editor = Unicode(get_default_editor(), config=True,
281 help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
281 help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
282 )
282 )
283 pager = Unicode('less', config=True,
283 pager = Unicode('less', config=True,
284 help="The shell program to be used for paging.")
284 help="The shell program to be used for paging.")
285
285
286 screen_length = Integer(0, config=True,
286 screen_length = Integer(0, config=True,
287 help=
287 help=
288 """Number of lines of your screen, used to control printing of very
288 """Number of lines of your screen, used to control printing of very
289 long strings. Strings longer than this number of lines will be sent
289 long strings. Strings longer than this number of lines will be sent
290 through a pager instead of directly printed. The default value for
290 through a pager instead of directly printed. The default value for
291 this is 0, which means IPython will auto-detect your screen size every
291 this is 0, which means IPython will auto-detect your screen size every
292 time it needs to print certain potentially long strings (this doesn't
292 time it needs to print certain potentially long strings (this doesn't
293 change the behavior of the 'print' keyword, it's only triggered
293 change the behavior of the 'print' keyword, it's only triggered
294 internally). If for some reason this isn't working well (it needs
294 internally). If for some reason this isn't working well (it needs
295 curses support), specify it yourself. Otherwise don't change the
295 curses support), specify it yourself. Otherwise don't change the
296 default.""",
296 default.""",
297 )
297 )
298 term_title = CBool(False, config=True,
298 term_title = CBool(False, config=True,
299 help="Enable auto setting the terminal title."
299 help="Enable auto setting the terminal title."
300 )
300 )
301
301
302 # In the terminal, GUI control is done via PyOS_InputHook
302 # In the terminal, GUI control is done via PyOS_InputHook
303 from IPython.lib.inputhook import enable_gui
303 from IPython.lib.inputhook import enable_gui
304 enable_gui = staticmethod(enable_gui)
304 enable_gui = staticmethod(enable_gui)
305
305
306 def __init__(self, config=None, ipython_dir=None, profile_dir=None,
306 def __init__(self, config=None, ipython_dir=None, profile_dir=None,
307 user_ns=None, user_module=None, custom_exceptions=((),None),
307 user_ns=None, user_module=None, custom_exceptions=((),None),
308 usage=None, banner1=None, banner2=None, display_banner=None):
308 usage=None, banner1=None, banner2=None, display_banner=None):
309
309
310 super(TerminalInteractiveShell, self).__init__(
310 super(TerminalInteractiveShell, self).__init__(
311 config=config, profile_dir=profile_dir, user_ns=user_ns,
311 config=config, profile_dir=profile_dir, user_ns=user_ns,
312 user_module=user_module, custom_exceptions=custom_exceptions
312 user_module=user_module, custom_exceptions=custom_exceptions
313 )
313 )
314 # use os.system instead of utils.process.system by default,
314 # use os.system instead of utils.process.system by default,
315 # because piped system doesn't make sense in the Terminal:
315 # because piped system doesn't make sense in the Terminal:
316 self.system = self.system_raw
316 self.system = self.system_raw
317
317
318 self.init_term_title()
318 self.init_term_title()
319 self.init_usage(usage)
319 self.init_usage(usage)
320 self.init_banner(banner1, banner2, display_banner)
320 self.init_banner(banner1, banner2, display_banner)
321
321
322 #-------------------------------------------------------------------------
322 #-------------------------------------------------------------------------
323 # Things related to the terminal
323 # Things related to the terminal
324 #-------------------------------------------------------------------------
324 #-------------------------------------------------------------------------
325
325
326 @property
326 @property
327 def usable_screen_length(self):
327 def usable_screen_length(self):
328 if self.screen_length == 0:
328 if self.screen_length == 0:
329 return 0
329 return 0
330 else:
330 else:
331 num_lines_bot = self.separate_in.count('\n')+1
331 num_lines_bot = self.separate_in.count('\n')+1
332 return self.screen_length - num_lines_bot
332 return self.screen_length - num_lines_bot
333
333
334 def init_term_title(self):
334 def init_term_title(self):
335 # Enable or disable the terminal title.
335 # Enable or disable the terminal title.
336 if self.term_title:
336 if self.term_title:
337 toggle_set_term_title(True)
337 toggle_set_term_title(True)
338 set_term_title('IPython: ' + abbrev_cwd())
338 set_term_title('IPython: ' + abbrev_cwd())
339 else:
339 else:
340 toggle_set_term_title(False)
340 toggle_set_term_title(False)
341
341
342 #-------------------------------------------------------------------------
342 #-------------------------------------------------------------------------
343 # Things related to aliases
343 # Things related to aliases
344 #-------------------------------------------------------------------------
344 #-------------------------------------------------------------------------
345
345
346 def init_alias(self):
346 def init_alias(self):
347 # The parent class defines aliases that can be safely used with any
347 # The parent class defines aliases that can be safely used with any
348 # frontend.
348 # frontend.
349 super(TerminalInteractiveShell, self).init_alias()
349 super(TerminalInteractiveShell, self).init_alias()
350
350
351 # Now define aliases that only make sense on the terminal, because they
351 # Now define aliases that only make sense on the terminal, because they
352 # need direct access to the console in a way that we can't emulate in
352 # need direct access to the console in a way that we can't emulate in
353 # GUI or web frontend
353 # GUI or web frontend
354 if os.name == 'posix':
354 if os.name == 'posix':
355 aliases = [('clear', 'clear'), ('more', 'more'), ('less', 'less'),
355 aliases = [('clear', 'clear'), ('more', 'more'), ('less', 'less'),
356 ('man', 'man')]
356 ('man', 'man')]
357 elif os.name == 'nt':
357 elif os.name == 'nt':
358 aliases = [('cls', 'cls')]
358 aliases = [('cls', 'cls')]
359
359
360
360
361 for name, cmd in aliases:
361 for name, cmd in aliases:
362 self.alias_manager.define_alias(name, cmd)
362 self.alias_manager.define_alias(name, cmd)
363
363
364 #-------------------------------------------------------------------------
364 #-------------------------------------------------------------------------
365 # Things related to the banner and usage
365 # Things related to the banner and usage
366 #-------------------------------------------------------------------------
366 #-------------------------------------------------------------------------
367
367
368 def _banner1_changed(self):
368 def _banner1_changed(self):
369 self.compute_banner()
369 self.compute_banner()
370
370
371 def _banner2_changed(self):
371 def _banner2_changed(self):
372 self.compute_banner()
372 self.compute_banner()
373
373
374 def _term_title_changed(self, name, new_value):
374 def _term_title_changed(self, name, new_value):
375 self.init_term_title()
375 self.init_term_title()
376
376
377 def init_banner(self, banner1, banner2, display_banner):
377 def init_banner(self, banner1, banner2, display_banner):
378 if banner1 is not None:
378 if banner1 is not None:
379 self.banner1 = banner1
379 self.banner1 = banner1
380 if banner2 is not None:
380 if banner2 is not None:
381 self.banner2 = banner2
381 self.banner2 = banner2
382 if display_banner is not None:
382 if display_banner is not None:
383 self.display_banner = display_banner
383 self.display_banner = display_banner
384 self.compute_banner()
384 self.compute_banner()
385
385
386 def show_banner(self, banner=None):
386 def show_banner(self, banner=None):
387 if banner is None:
387 if banner is None:
388 banner = self.banner
388 banner = self.banner
389 self.write(banner)
389 self.write(banner)
390
390
391 def compute_banner(self):
391 def compute_banner(self):
392 self.banner = self.banner1
392 self.banner = self.banner1
393 if self.profile and self.profile != 'default':
393 if self.profile and self.profile != 'default':
394 self.banner += '\nIPython profile: %s\n' % self.profile
394 self.banner += '\nIPython profile: %s\n' % self.profile
395 if self.banner2:
395 if self.banner2:
396 self.banner += '\n' + self.banner2
396 self.banner += '\n' + self.banner2
397
397
398 def init_usage(self, usage=None):
398 def init_usage(self, usage=None):
399 if usage is None:
399 if usage is None:
400 self.usage = interactive_usage
400 self.usage = interactive_usage
401 else:
401 else:
402 self.usage = usage
402 self.usage = usage
403
403
404 #-------------------------------------------------------------------------
404 #-------------------------------------------------------------------------
405 # Mainloop and code execution logic
405 # Mainloop and code execution logic
406 #-------------------------------------------------------------------------
406 #-------------------------------------------------------------------------
407
407
408 def mainloop(self, display_banner=None):
408 def mainloop(self, display_banner=None):
409 """Start the mainloop.
409 """Start the mainloop.
410
410
411 If an optional banner argument is given, it will override the
411 If an optional banner argument is given, it will override the
412 internally created default banner.
412 internally created default banner.
413 """
413 """
414
414
415 with nested(self.builtin_trap, self.display_trap):
415 with nested(self.builtin_trap, self.display_trap):
416
416
417 while 1:
417 while 1:
418 try:
418 try:
419 self.interact(display_banner=display_banner)
419 self.interact(display_banner=display_banner)
420 #self.interact_with_readline()
420 #self.interact_with_readline()
421 # XXX for testing of a readline-decoupled repl loop, call
421 # XXX for testing of a readline-decoupled repl loop, call
422 # interact_with_readline above
422 # interact_with_readline above
423 break
423 break
424 except KeyboardInterrupt:
424 except KeyboardInterrupt:
425 # this should not be necessary, but KeyboardInterrupt
425 # this should not be necessary, but KeyboardInterrupt
426 # handling seems rather unpredictable...
426 # handling seems rather unpredictable...
427 self.write("\nKeyboardInterrupt in interact()\n")
427 self.write("\nKeyboardInterrupt in interact()\n")
428
428
429 def _replace_rlhist_multiline(self, source_raw, hlen_before_cell):
429 def _replace_rlhist_multiline(self, source_raw, hlen_before_cell):
430 """Store multiple lines as a single entry in history"""
430 """Store multiple lines as a single entry in history"""
431
431
432 # do nothing without readline or disabled multiline
432 # do nothing without readline or disabled multiline
433 if not self.has_readline or not self.multiline_history:
433 if not self.has_readline or not self.multiline_history:
434 return hlen_before_cell
434 return hlen_before_cell
435
435
436 # windows rl has no remove_history_item
436 # windows rl has no remove_history_item
437 if not hasattr(self.readline, "remove_history_item"):
437 if not hasattr(self.readline, "remove_history_item"):
438 return hlen_before_cell
438 return hlen_before_cell
439
439
440 # skip empty cells
440 # skip empty cells
441 if not source_raw.rstrip():
441 if not source_raw.rstrip():
442 return hlen_before_cell
442 return hlen_before_cell
443
443
444 # nothing changed do nothing, e.g. when rl removes consecutive dups
444 # nothing changed do nothing, e.g. when rl removes consecutive dups
445 hlen = self.readline.get_current_history_length()
445 hlen = self.readline.get_current_history_length()
446 if hlen == hlen_before_cell:
446 if hlen == hlen_before_cell:
447 return hlen_before_cell
447 return hlen_before_cell
448
448
449 for i in range(hlen - hlen_before_cell):
449 for i in range(hlen - hlen_before_cell):
450 self.readline.remove_history_item(hlen - i - 1)
450 self.readline.remove_history_item(hlen - i - 1)
451 stdin_encoding = get_stream_enc(sys.stdin, 'utf-8')
451 stdin_encoding = get_stream_enc(sys.stdin, 'utf-8')
452 self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(),
452 self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(),
453 stdin_encoding))
453 stdin_encoding))
454 return self.readline.get_current_history_length()
454 return self.readline.get_current_history_length()
455
455
456 def interact(self, display_banner=None):
456 def interact(self, display_banner=None):
457 """Closely emulate the interactive Python console."""
457 """Closely emulate the interactive Python console."""
458
458
459 # batch run -> do not interact
459 # batch run -> do not interact
460 if self.exit_now:
460 if self.exit_now:
461 return
461 return
462
462
463 if display_banner is None:
463 if display_banner is None:
464 display_banner = self.display_banner
464 display_banner = self.display_banner
465
465
466 if isinstance(display_banner, basestring):
466 if isinstance(display_banner, basestring):
467 self.show_banner(display_banner)
467 self.show_banner(display_banner)
468 elif display_banner:
468 elif display_banner:
469 self.show_banner()
469 self.show_banner()
470
470
471 more = False
471 more = False
472
472
473 if self.has_readline:
473 if self.has_readline:
474 self.readline_startup_hook(self.pre_readline)
474 self.readline_startup_hook(self.pre_readline)
475 hlen_b4_cell = self.readline.get_current_history_length()
475 hlen_b4_cell = self.readline.get_current_history_length()
476 else:
476 else:
477 hlen_b4_cell = 0
477 hlen_b4_cell = 0
478 # exit_now is set by a call to %Exit or %Quit, through the
478 # exit_now is set by a call to %Exit or %Quit, through the
479 # ask_exit callback.
479 # ask_exit callback.
480
480
481 while not self.exit_now:
481 while not self.exit_now:
482 self.hooks.pre_prompt_hook()
482 self.hooks.pre_prompt_hook()
483 if more:
483 if more:
484 try:
484 try:
485 prompt = self.prompt_manager.render('in2')
485 prompt = self.prompt_manager.render('in2')
486 except:
486 except:
487 self.showtraceback()
487 self.showtraceback()
488 if self.autoindent:
488 if self.autoindent:
489 self.rl_do_indent = True
489 self.rl_do_indent = True
490
490
491 else:
491 else:
492 try:
492 try:
493 prompt = self.separate_in + self.prompt_manager.render('in')
493 prompt = self.separate_in + self.prompt_manager.render('in')
494 except:
494 except:
495 self.showtraceback()
495 self.showtraceback()
496 try:
496 try:
497 line = self.raw_input(prompt)
497 line = self.raw_input(prompt)
498 if self.exit_now:
498 if self.exit_now:
499 # quick exit on sys.std[in|out] close
499 # quick exit on sys.std[in|out] close
500 break
500 break
501 if self.autoindent:
501 if self.autoindent:
502 self.rl_do_indent = False
502 self.rl_do_indent = False
503
503
504 except KeyboardInterrupt:
504 except KeyboardInterrupt:
505 #double-guard against keyboardinterrupts during kbdint handling
505 #double-guard against keyboardinterrupts during kbdint handling
506 try:
506 try:
507 self.write('\nKeyboardInterrupt\n')
507 self.write('\nKeyboardInterrupt\n')
508 source_raw = self.input_splitter.source_raw_reset()[1]
508 source_raw = self.input_splitter.source_raw_reset()[1]
509 hlen_b4_cell = \
509 hlen_b4_cell = \
510 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
510 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
511 more = False
511 more = False
512 except KeyboardInterrupt:
512 except KeyboardInterrupt:
513 pass
513 pass
514 except EOFError:
514 except EOFError:
515 if self.autoindent:
515 if self.autoindent:
516 self.rl_do_indent = False
516 self.rl_do_indent = False
517 if self.has_readline:
517 if self.has_readline:
518 self.readline_startup_hook(None)
518 self.readline_startup_hook(None)
519 self.write('\n')
519 self.write('\n')
520 self.exit()
520 self.exit()
521 except bdb.BdbQuit:
521 except bdb.BdbQuit:
522 warn('The Python debugger has exited with a BdbQuit exception.\n'
522 warn('The Python debugger has exited with a BdbQuit exception.\n'
523 'Because of how pdb handles the stack, it is impossible\n'
523 'Because of how pdb handles the stack, it is impossible\n'
524 'for IPython to properly format this particular exception.\n'
524 'for IPython to properly format this particular exception.\n'
525 'IPython will resume normal operation.')
525 'IPython will resume normal operation.')
526 except:
526 except:
527 # exceptions here are VERY RARE, but they can be triggered
527 # exceptions here are VERY RARE, but they can be triggered
528 # asynchronously by signal handlers, for example.
528 # asynchronously by signal handlers, for example.
529 self.showtraceback()
529 self.showtraceback()
530 else:
530 else:
531 self.input_splitter.push(line)
531 self.input_splitter.push(line)
532 more = self.input_splitter.push_accepts_more()
532 more = self.input_splitter.push_accepts_more()
533 if (self.SyntaxTB.last_syntax_error and
533 if (self.SyntaxTB.last_syntax_error and
534 self.autoedit_syntax):
534 self.autoedit_syntax):
535 self.edit_syntax_error()
535 self.edit_syntax_error()
536 if not more:
536 if not more:
537 source_raw = self.input_splitter.source_raw_reset()[1]
537 source_raw = self.input_splitter.source_raw_reset()[1]
538 self.run_cell(source_raw, store_history=True)
538 self.run_cell(source_raw, store_history=True)
539 hlen_b4_cell = \
539 hlen_b4_cell = \
540 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
540 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
541
541
542 # Turn off the exit flag, so the mainloop can be restarted if desired
542 # Turn off the exit flag, so the mainloop can be restarted if desired
543 self.exit_now = False
543 self.exit_now = False
544
544
545 def raw_input(self, prompt=''):
545 def raw_input(self, prompt=''):
546 """Write a prompt and read a line.
546 """Write a prompt and read a line.
547
547
548 The returned line does not include the trailing newline.
548 The returned line does not include the trailing newline.
549 When the user enters the EOF key sequence, EOFError is raised.
549 When the user enters the EOF key sequence, EOFError is raised.
550
550
551 Optional inputs:
551 Optional inputs:
552
552
553 - prompt(''): a string to be printed to prompt the user.
553 - prompt(''): a string to be printed to prompt the user.
554
554
555 - continue_prompt(False): whether this line is the first one or a
555 - continue_prompt(False): whether this line is the first one or a
556 continuation in a sequence of inputs.
556 continuation in a sequence of inputs.
557 """
557 """
558 # Code run by the user may have modified the readline completer state.
558 # Code run by the user may have modified the readline completer state.
559 # We must ensure that our completer is back in place.
559 # We must ensure that our completer is back in place.
560
560
561 if self.has_readline:
561 if self.has_readline:
562 self.set_readline_completer()
562 self.set_readline_completer()
563
563
564 try:
564 try:
565 line = py3compat.str_to_unicode(self.raw_input_original(prompt))
565 line = py3compat.str_to_unicode(self.raw_input_original(prompt))
566 except ValueError:
566 except ValueError:
567 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
567 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
568 " or sys.stdout.close()!\nExiting IPython!")
568 " or sys.stdout.close()!\nExiting IPython!")
569 self.ask_exit()
569 self.ask_exit()
570 return ""
570 return ""
571
571
572 # Try to be reasonably smart about not re-indenting pasted input more
572 # Try to be reasonably smart about not re-indenting pasted input more
573 # than necessary. We do this by trimming out the auto-indent initial
573 # than necessary. We do this by trimming out the auto-indent initial
574 # spaces, if the user's actual input started itself with whitespace.
574 # spaces, if the user's actual input started itself with whitespace.
575 if self.autoindent:
575 if self.autoindent:
576 if num_ini_spaces(line) > self.indent_current_nsp:
576 if num_ini_spaces(line) > self.indent_current_nsp:
577 line = line[self.indent_current_nsp:]
577 line = line[self.indent_current_nsp:]
578 self.indent_current_nsp = 0
578 self.indent_current_nsp = 0
579
579
580 return line
580 return line
581
581
582 #-------------------------------------------------------------------------
582 #-------------------------------------------------------------------------
583 # Methods to support auto-editing of SyntaxErrors.
583 # Methods to support auto-editing of SyntaxErrors.
584 #-------------------------------------------------------------------------
584 #-------------------------------------------------------------------------
585
585
586 def edit_syntax_error(self):
586 def edit_syntax_error(self):
587 """The bottom half of the syntax error handler called in the main loop.
587 """The bottom half of the syntax error handler called in the main loop.
588
588
589 Loop until syntax error is fixed or user cancels.
589 Loop until syntax error is fixed or user cancels.
590 """
590 """
591
591
592 while self.SyntaxTB.last_syntax_error:
592 while self.SyntaxTB.last_syntax_error:
593 # copy and clear last_syntax_error
593 # copy and clear last_syntax_error
594 err = self.SyntaxTB.clear_err_state()
594 err = self.SyntaxTB.clear_err_state()
595 if not self._should_recompile(err):
595 if not self._should_recompile(err):
596 return
596 return
597 try:
597 try:
598 # may set last_syntax_error again if a SyntaxError is raised
598 # may set last_syntax_error again if a SyntaxError is raised
599 self.safe_execfile(err.filename,self.user_ns)
599 self.safe_execfile(err.filename,self.user_ns)
600 except:
600 except:
601 self.showtraceback()
601 self.showtraceback()
602 else:
602 else:
603 try:
603 try:
604 f = open(err.filename)
604 f = open(err.filename)
605 try:
605 try:
606 # This should be inside a display_trap block and I
606 # This should be inside a display_trap block and I
607 # think it is.
607 # think it is.
608 sys.displayhook(f.read())
608 sys.displayhook(f.read())
609 finally:
609 finally:
610 f.close()
610 f.close()
611 except:
611 except:
612 self.showtraceback()
612 self.showtraceback()
613
613
614 def _should_recompile(self,e):
614 def _should_recompile(self,e):
615 """Utility routine for edit_syntax_error"""
615 """Utility routine for edit_syntax_error"""
616
616
617 if e.filename in ('<ipython console>','<input>','<string>',
617 if e.filename in ('<ipython console>','<input>','<string>',
618 '<console>','<BackgroundJob compilation>',
618 '<console>','<BackgroundJob compilation>',
619 None):
619 None):
620
620
621 return False
621 return False
622 try:
622 try:
623 if (self.autoedit_syntax and
623 if (self.autoedit_syntax and
624 not self.ask_yes_no('Return to editor to correct syntax error? '
624 not self.ask_yes_no('Return to editor to correct syntax error? '
625 '[Y/n] ','y')):
625 '[Y/n] ','y')):
626 return False
626 return False
627 except EOFError:
627 except EOFError:
628 return False
628 return False
629
629
630 def int0(x):
630 def int0(x):
631 try:
631 try:
632 return int(x)
632 return int(x)
633 except TypeError:
633 except TypeError:
634 return 0
634 return 0
635 # always pass integer line and offset values to editor hook
635 # always pass integer line and offset values to editor hook
636 try:
636 try:
637 self.hooks.fix_error_editor(e.filename,
637 self.hooks.fix_error_editor(e.filename,
638 int0(e.lineno),int0(e.offset),e.msg)
638 int0(e.lineno),int0(e.offset),e.msg)
639 except TryNext:
639 except TryNext:
640 warn('Could not open editor')
640 warn('Could not open editor')
641 return False
641 return False
642 return True
642 return True
643
643
644 #-------------------------------------------------------------------------
644 #-------------------------------------------------------------------------
645 # Things related to exiting
645 # Things related to exiting
646 #-------------------------------------------------------------------------
646 #-------------------------------------------------------------------------
647
647
648 def ask_exit(self):
648 def ask_exit(self):
649 """ Ask the shell to exit. Can be overiden and used as a callback. """
649 """ Ask the shell to exit. Can be overiden and used as a callback. """
650 self.exit_now = True
650 self.exit_now = True
651
651
652 def exit(self):
652 def exit(self):
653 """Handle interactive exit.
653 """Handle interactive exit.
654
654
655 This method calls the ask_exit callback."""
655 This method calls the ask_exit callback."""
656 if self.confirm_exit:
656 if self.confirm_exit:
657 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
657 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
658 self.ask_exit()
658 self.ask_exit()
659 else:
659 else:
660 self.ask_exit()
660 self.ask_exit()
661
661
662 #-------------------------------------------------------------------------
662 #-------------------------------------------------------------------------
663 # Things related to magics
663 # Things related to magics
664 #-------------------------------------------------------------------------
664 #-------------------------------------------------------------------------
665
665
666 def init_magics(self):
666 def init_magics(self):
667 super(TerminalInteractiveShell, self).init_magics()
667 super(TerminalInteractiveShell, self).init_magics()
668 self.register_magics(TerminalMagics)
668 self.register_magics(TerminalMagics)
669
669
670 def showindentationerror(self):
670 def showindentationerror(self):
671 super(TerminalInteractiveShell, self).showindentationerror()
671 super(TerminalInteractiveShell, self).showindentationerror()
672 print("If you want to paste code into IPython, try the "
672 print("If you want to paste code into IPython, try the "
673 "%paste and %cpaste magic functions.")
673 "%paste and %cpaste magic functions.")
674
674
675
675
676 InteractiveShellABC.register(TerminalInteractiveShell)
676 InteractiveShellABC.register(TerminalInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now