##// END OF EJS Templates
Renamed @register_magics to @magics_class to avoid confusion....
Fernando Perez -
Show More
@@ -1,437 +1,437 b''
1 1 # encoding: utf-8
2 2 """Magic functions for InteractiveShell.
3 3 """
4 4
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
7 7 # Copyright (C) 2001 Fernando Perez <fperez@colorado.edu>
8 8 # Copyright (C) 2008 The IPython Development Team
9 9
10 10 # Distributed under the terms of the BSD License. The full license is in
11 11 # the file COPYING, distributed as part of this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17 # Stdlib
18 18 import os
19 19 import re
20 20 import sys
21 21 import types
22 22 from getopt import getopt, GetoptError
23 23
24 24 # Our own
25 25 from IPython.config.configurable import Configurable
26 26 from IPython.core import oinspect
27 27 from IPython.core.error import UsageError
28 28 from IPython.core.prefilter import ESC_MAGIC
29 29 from IPython.external.decorator import decorator
30 30 from IPython.utils.ipstruct import Struct
31 31 from IPython.utils.process import arg_split
32 32 from IPython.utils.traitlets import Bool, Dict, Instance
33 33 from IPython.utils.warn import error, warn
34 34
35 35 #-----------------------------------------------------------------------------
36 36 # Globals
37 37 #-----------------------------------------------------------------------------
38 38
39 39 # A dict we'll use for each class that has magics, used as temporary storage to
40 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 42 # access to the class when they run. See for more details:
43 43 # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class
44 44
45 45 magics = dict(line={}, cell={})
46 46
47 47 magic_types = ('line', 'cell')
48 48 magic_spec = ('line', 'cell', 'line_cell')
49 49
50 50 #-----------------------------------------------------------------------------
51 51 # Utility classes and functions
52 52 #-----------------------------------------------------------------------------
53 53
54 54 class Bunch: pass
55 55
56 56
57 57 def on_off(tag):
58 58 """Return an ON/OFF string for a 1/0 input. Simple utility function."""
59 59 return ['OFF','ON'][tag]
60 60
61 61
62 62 def compress_dhist(dh):
63 63 head, tail = dh[:-10], dh[-10:]
64 64
65 65 newhead = []
66 66 done = set()
67 67 for h in head:
68 68 if h in done:
69 69 continue
70 70 newhead.append(h)
71 71 done.add(h)
72 72
73 73 return newhead + tail
74 74
75 75
76 76 def needs_local_scope(func):
77 77 """Decorator to mark magic functions which need to local scope to run."""
78 78 func.needs_local_scope = True
79 79 return func
80 80
81 81 #-----------------------------------------------------------------------------
82 82 # Class and method decorators for registering magics
83 83 #-----------------------------------------------------------------------------
84 84
85 def register_magics(cls):
85 def magics_class(cls):
86 86 cls.registered = True
87 87 cls.magics = dict(line = magics['line'],
88 88 cell = magics['cell'])
89 89 magics['line'] = {}
90 90 magics['cell'] = {}
91 91 return cls
92 92
93 93
94 94 def record_magic(dct, mtype, mname, func):
95 95 if mtype == 'line_cell':
96 96 dct['line'][mname] = dct['cell'][mname] = func
97 97 else:
98 98 dct[mtype][mname] = func
99 99
100 100
101 101 def validate_type(magic_type):
102 102 if magic_type not in magic_spec:
103 103 raise ValueError('magic_type must be one of %s, %s given' %
104 104 magic_types, magic_type)
105 105
106 106
107 107 def _magic_marker(magic_type):
108 108 validate_type(magic_type)
109 109
110 110 # This is a closure to capture the magic_type. We could also use a class,
111 111 # but it's overkill for just that one bit of state.
112 112 def magic_deco(arg):
113 113 call = lambda f, *a, **k: f(*a, **k)
114 114
115 115 if callable(arg):
116 116 # "Naked" decorator call (just @foo, no args)
117 117 func = arg
118 118 name = func.func_name
119 119 func.magic_name = name
120 120 retval = decorator(call, func)
121 121 record_magic(magics, magic_type, name, name)
122 122 elif isinstance(arg, basestring):
123 123 # Decorator called with arguments (@foo('bar'))
124 124 name = arg
125 125 def mark(func, *a, **kw):
126 126 func.magic_name = name
127 127 record_magic(magics, magic_type, name, func.func_name)
128 128 return decorator(call, func)
129 129 retval = mark
130 130 else:
131 131 raise ValueError("Decorator can only be called with "
132 132 "string or function")
133 133 return retval
134 134
135 135 return magic_deco
136 136
137 137
138 138 def _function_magic_marker(magic_type):
139 139 validate_type(magic_type)
140 140
141 141 # This is a closure to capture the magic_type. We could also use a class,
142 142 # but it's overkill for just that one bit of state.
143 143 def magic_deco(arg):
144 144 call = lambda f, *a, **k: f(*a, **k)
145 145
146 146 # Find get_ipython() in the caller's namespace
147 147 caller = sys._getframe(1)
148 148 for ns in ['f_locals', 'f_globals', 'f_builtins']:
149 149 get_ipython = getattr(caller, ns).get('get_ipython')
150 150 if get_ipython is not None:
151 151 break
152 152 else:
153 153 raise('Decorator can only run in context where `get_ipython` exists')
154 154
155 155 ip = get_ipython()
156 156
157 157 if callable(arg):
158 158 # "Naked" decorator call (just @foo, no args)
159 159 func = arg
160 160 #name = func.func_name
161 161 #func.magic_name = name
162 162 ip.register_magic_function(func)
163 163 retval = decorator(call, func)
164 164 elif isinstance(arg, basestring):
165 165 # Decorator called with arguments (@foo('bar'))
166 166 name = arg
167 167 def mark(func, *a, **kw):
168 168 #func.magic_name = name
169 169 ip.register_magic_function(func)
170 170 return decorator(call, func)
171 171 retval = mark
172 172 else:
173 173 raise ValueError("Decorator can only be called with "
174 174 "string or function")
175 175 return retval
176 176
177 177 return magic_deco
178 178
179 179
180 180 # Create the actual decorators for public use
181 181
182 182 # These three are used to decorate methods in class definitions
183 183 line_magic = _magic_marker('line')
184 184 cell_magic = _magic_marker('cell')
185 185 line_cell_magic = _magic_marker('line_cell')
186 186
187 187 # These three decorate standalone functions and perform the decoration
188 188 # immediately. They can only run where get_ipython() works
189 189 register_line_magic = _function_magic_marker('line')
190 190 register_cell_magic = _function_magic_marker('cell')
191 191 register_line_cell_magic = _function_magic_marker('line_cell')
192 192
193 193 #-----------------------------------------------------------------------------
194 194 # Core Magic classes
195 195 #-----------------------------------------------------------------------------
196 196
197 197 class MagicsManager(Configurable):
198 198 """Object that handles all magic-related functionality for IPython.
199 199 """
200 200 # Non-configurable class attributes
201 201
202 202 # A two-level dict, first keyed by magic type, then by magic function, and
203 203 # holding the actual callable object as value. This is the dict used for
204 204 # magic function dispatch
205 205 magics = Dict
206 206
207 207 # A registry of the original objects that we've been given holding magics.
208 208 registry = Dict
209 209
210 210 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
211 211
212 212 auto_magic = Bool
213 213
214 214 _auto_status = [
215 215 'Automagic is OFF, % prefix IS needed for magic functions.',
216 216 'Automagic is ON, % prefix IS NOT needed for magic functions.']
217 217
218 218 user_magics = Instance('IPython.core.magics.UserMagics')
219 219
220 220 def __init__(self, shell=None, config=None, user_magics=None, **traits):
221 221
222 222 super(MagicsManager, self).__init__(shell=shell, config=config,
223 223 user_magics=user_magics, **traits)
224 224 self.magics = dict(line={}, cell={})
225 225 # Let's add the user_magics to the registry for uniformity, so *all*
226 226 # registered magic containers can be found there.
227 227 self.registry[user_magics.__class__.__name__] = user_magics
228 228
229 229 def auto_status(self):
230 230 """Return descriptive string with automagic status."""
231 231 return self._auto_status[self.auto_magic]
232 232
233 233 def lsmagic(self):
234 234 """Return a dict of currently available magic functions.
235 235
236 236 The return dict has the keys 'line' and 'cell', corresponding to the
237 237 two types of magics we support. Each value is a list of names.
238 238 """
239 239 return self.magics
240 240
241 241 def register(self, *magic_objects):
242 242 """Register one or more instances of Magics.
243 243 """
244 244 # Start by validating them to ensure they have all had their magic
245 245 # methods registered at the instance level
246 246 for m in magic_objects:
247 247 if not m.registered:
248 248 raise ValueError("Class of magics %r was constructed without "
249 249 "the @register_macics class decorator")
250 250 if type(m) is type:
251 251 # If we're given an uninstantiated class
252 252 m = m(self.shell)
253 253
254 254 # Now that we have an instance, we can register it and update the
255 255 # table of callables
256 256 self.registry[m.__class__.__name__] = m
257 257 for mtype in magic_types:
258 258 self.magics[mtype].update(m.magics[mtype])
259 259
260 260 def register_function(self, func, magic_type='line', magic_name=None):
261 261 """Expose a standalone function as magic function for ipython.
262 262 """
263 263
264 264 # Create the new method in the user_magics and register it in the
265 265 # global table
266 266 validate_type(magic_type)
267 267 magic_name = func.func_name if magic_name is None else magic_name
268 268 setattr(self.user_magics, magic_name, func)
269 269 record_magic(self.magics, magic_type, magic_name, func)
270 270
271 271 def define_magic(self, name, func):
272 272 """Support for deprecated API.
273 273
274 274 This method exists only to support the old-style definition of magics.
275 275 It will eventually be removed. Deliberately not documented further.
276 276 """
277 277 meth = types.MethodType(func, self.user_magics)
278 278 setattr(self.user_magics, name, meth)
279 279 record_magic(self.magics, 'line', name, meth)
280 280
281 281 # Key base class that provides the central functionality for magics.
282 282
283 283 class Magics(object):
284 284 """Base class for implementing magic functions.
285 285
286 286 Shell functions which can be reached as %function_name. All magic
287 287 functions should accept a string, which they can parse for their own
288 288 needs. This can make some functions easier to type, eg `%cd ../`
289 289 vs. `%cd("../")`
290 290
291 291 Classes providing magic functions need to subclass this class, and they
292 292 MUST:
293 293
294 294 - Use the method decorators `@line_magic` and `@cell_magic` to decorate
295 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 298 methods are properly registered at the instance level upon instance
299 299 initialization.
300 300
301 301 See :mod:`magic_functions` for examples of actual implementation classes.
302 302 """
303 303 # Dict holding all command-line options for each magic.
304 304 options_table = None
305 305 # Dict for the mapping of magic names to methods, set by class decorator
306 306 magics = None
307 307 # Flag to check that the class decorator was properly applied
308 308 registered = False
309 309 # Instance of IPython shell
310 310 shell = None
311 311
312 312 def __init__(self, shell):
313 313 if not(self.__class__.registered):
314 314 raise ValueError('Magics subclass without registration - '
315 'did you forget to apply @register_magics?')
315 'did you forget to apply @magics_class?')
316 316 self.shell = shell
317 317 self.options_table = {}
318 318 # The method decorators are run when the instance doesn't exist yet, so
319 319 # they can only record the names of the methods they are supposed to
320 320 # grab. Only now, that the instance exists, can we create the proper
321 321 # mapping to bound methods. So we read the info off the original names
322 322 # table and replace each method name by the actual bound method.
323 323 for mtype in magic_types:
324 324 tab = self.magics[mtype]
325 325 # must explicitly use keys, as we're mutating this puppy
326 326 for magic_name in tab.keys():
327 327 meth_name = tab[magic_name]
328 328 if isinstance(meth_name, basestring):
329 329 tab[magic_name] = getattr(self, meth_name)
330 330
331 331 def arg_err(self,func):
332 332 """Print docstring if incorrect arguments were passed"""
333 333 print 'Error in arguments:'
334 334 print oinspect.getdoc(func)
335 335
336 336 def format_latex(self, strng):
337 337 """Format a string for latex inclusion."""
338 338
339 339 # Characters that need to be escaped for latex:
340 340 escape_re = re.compile(r'(%|_|\$|#|&)',re.MULTILINE)
341 341 # Magic command names as headers:
342 342 cmd_name_re = re.compile(r'^(%s.*?):' % ESC_MAGIC,
343 343 re.MULTILINE)
344 344 # Magic commands
345 345 cmd_re = re.compile(r'(?P<cmd>%s.+?\b)(?!\}\}:)' % ESC_MAGIC,
346 346 re.MULTILINE)
347 347 # Paragraph continue
348 348 par_re = re.compile(r'\\$',re.MULTILINE)
349 349
350 350 # The "\n" symbol
351 351 newline_re = re.compile(r'\\n')
352 352
353 353 # Now build the string for output:
354 354 #strng = cmd_name_re.sub(r'\n\\texttt{\\textsl{\\large \1}}:',strng)
355 355 strng = cmd_name_re.sub(r'\n\\bigskip\n\\texttt{\\textbf{ \1}}:',
356 356 strng)
357 357 strng = cmd_re.sub(r'\\texttt{\g<cmd>}',strng)
358 358 strng = par_re.sub(r'\\\\',strng)
359 359 strng = escape_re.sub(r'\\\1',strng)
360 360 strng = newline_re.sub(r'\\textbackslash{}n',strng)
361 361 return strng
362 362
363 363 def parse_options(self, arg_str, opt_str, *long_opts, **kw):
364 364 """Parse options passed to an argument string.
365 365
366 366 The interface is similar to that of getopt(), but it returns back a
367 367 Struct with the options as keys and the stripped argument string still
368 368 as a string.
369 369
370 370 arg_str is quoted as a true sys.argv vector by using shlex.split.
371 371 This allows us to easily expand variables, glob files, quote
372 372 arguments, etc.
373 373
374 374 Options:
375 375 -mode: default 'string'. If given as 'list', the argument string is
376 376 returned as a list (split on whitespace) instead of a string.
377 377
378 378 -list_all: put all option values in lists. Normally only options
379 379 appearing more than once are put in a list.
380 380
381 381 -posix (True): whether to split the input line in POSIX mode or not,
382 382 as per the conventions outlined in the shlex module from the
383 383 standard library."""
384 384
385 385 # inject default options at the beginning of the input line
386 386 caller = sys._getframe(1).f_code.co_name
387 387 arg_str = '%s %s' % (self.options_table.get(caller,''),arg_str)
388 388
389 389 mode = kw.get('mode','string')
390 390 if mode not in ['string','list']:
391 391 raise ValueError,'incorrect mode given: %s' % mode
392 392 # Get options
393 393 list_all = kw.get('list_all',0)
394 394 posix = kw.get('posix', os.name == 'posix')
395 395 strict = kw.get('strict', True)
396 396
397 397 # Check if we have more than one argument to warrant extra processing:
398 398 odict = {} # Dictionary with options
399 399 args = arg_str.split()
400 400 if len(args) >= 1:
401 401 # If the list of inputs only has 0 or 1 thing in it, there's no
402 402 # need to look for options
403 403 argv = arg_split(arg_str, posix, strict)
404 404 # Do regular option processing
405 405 try:
406 406 opts,args = getopt(argv,opt_str,*long_opts)
407 407 except GetoptError,e:
408 408 raise UsageError('%s ( allowed: "%s" %s)' % (e.msg,opt_str,
409 409 " ".join(long_opts)))
410 410 for o,a in opts:
411 411 if o.startswith('--'):
412 412 o = o[2:]
413 413 else:
414 414 o = o[1:]
415 415 try:
416 416 odict[o].append(a)
417 417 except AttributeError:
418 418 odict[o] = [odict[o],a]
419 419 except KeyError:
420 420 if list_all:
421 421 odict[o] = [a]
422 422 else:
423 423 odict[o] = a
424 424
425 425 # Prepare opts,args for return
426 426 opts = Struct(odict)
427 427 if mode == 'string':
428 428 args = ' '.join(args)
429 429
430 430 return opts,args
431 431
432 432 def default_option(self, fn, optstr):
433 433 """Make an entry in the options_table for fn, with value optstr"""
434 434
435 435 if fn not in self.lsmagic():
436 436 error("%s is not a magic function" % fn)
437 437 self.options_table[fn] = optstr
@@ -1,40 +1,40 b''
1 1 """Implementation of all the magic functions built into IPython.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 from ..magic import Magics, register_magics
15 from ..magic import Magics, magics_class
16 16 from .auto import AutoMagics
17 17 from .basic import BasicMagics
18 18 from .code import CodeMagics, MacroToEdit
19 19 from .config import ConfigMagics
20 20 from .deprecated import DeprecatedMagics
21 21 from .execution import ExecutionMagics
22 22 from .extension import ExtensionMagics
23 23 from .history import HistoryMagics
24 24 from .logging import LoggingMagics
25 25 from .namespace import NamespaceMagics
26 26 from .osm import OSMagics
27 27 from .pylab import PylabMagics
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Magic implementation classes
31 31 #-----------------------------------------------------------------------------
32 32
33 @register_magics
33 @magics_class
34 34 class UserMagics(Magics):
35 35 """Placeholder for user-defined magics to be added at runtime.
36 36
37 37 All magics are eventually merged into a single namespace at runtime, but we
38 38 use this class to isolate the magics defined dynamically by the user into
39 39 their own class.
40 40 """
@@ -1,128 +1,128 b''
1 1 """Implementation of magic functions that control various automatic behaviors.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 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 17 from IPython.testing.skipdoctest import skip_doctest
18 18 from IPython.utils.warn import error
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Magic implementation classes
22 22 #-----------------------------------------------------------------------------
23 23
24 @register_magics
24 @magics_class
25 25 class AutoMagics(Magics):
26 26 """Magics that control various autoX behaviors."""
27 27
28 28 def __init__(self, shell):
29 29 super(AutoMagics, self).__init__(shell)
30 30 # namespace for holding state we may need
31 31 self._magic_state = Bunch()
32 32
33 33 @line_magic
34 34 def automagic(self, parameter_s=''):
35 35 """Make magic functions callable without having to type the initial %.
36 36
37 37 Without argumentsl toggles on/off (when off, you must call it as
38 38 %automagic, of course). With arguments it sets the value, and you can
39 39 use any of (case insensitive):
40 40
41 41 - on, 1, True: to activate
42 42
43 43 - off, 0, False: to deactivate.
44 44
45 45 Note that magic functions have lowest priority, so if there's a
46 46 variable whose name collides with that of a magic fn, automagic won't
47 47 work for that function (you get the variable instead). However, if you
48 48 delete the variable (del var), the previously shadowed magic function
49 49 becomes visible to automagic again."""
50 50
51 51 arg = parameter_s.lower()
52 52 mman = self.shell.magics_manager
53 53 if arg in ('on', '1', 'true'):
54 54 val = True
55 55 elif arg in ('off', '0', 'false'):
56 56 val = False
57 57 else:
58 58 val = not mman.auto_magic
59 59 mman.auto_magic = val
60 60 print '\n' + self.shell.magics_manager.auto_status()
61 61
62 62 @skip_doctest
63 63 @line_magic
64 64 def autocall(self, parameter_s=''):
65 65 """Make functions callable without having to type parentheses.
66 66
67 67 Usage:
68 68
69 69 %autocall [mode]
70 70
71 71 The mode can be one of: 0->Off, 1->Smart, 2->Full. If not given, the
72 72 value is toggled on and off (remembering the previous state).
73 73
74 74 In more detail, these values mean:
75 75
76 76 0 -> fully disabled
77 77
78 78 1 -> active, but do not apply if there are no arguments on the line.
79 79
80 80 In this mode, you get::
81 81
82 82 In [1]: callable
83 83 Out[1]: <built-in function callable>
84 84
85 85 In [2]: callable 'hello'
86 86 ------> callable('hello')
87 87 Out[2]: False
88 88
89 89 2 -> Active always. Even if no arguments are present, the callable
90 90 object is called::
91 91
92 92 In [2]: float
93 93 ------> float()
94 94 Out[2]: 0.0
95 95
96 96 Note that even with autocall off, you can still use '/' at the start of
97 97 a line to treat the first argument on the command line as a function
98 98 and add parentheses to it::
99 99
100 100 In [8]: /str 43
101 101 ------> str(43)
102 102 Out[8]: '43'
103 103
104 104 # all-random (note for auto-testing)
105 105 """
106 106
107 107 if parameter_s:
108 108 arg = int(parameter_s)
109 109 else:
110 110 arg = 'toggle'
111 111
112 112 if not arg in (0, 1, 2,'toggle'):
113 113 error('Valid modes: (0->Off, 1->Smart, 2->Full')
114 114 return
115 115
116 116 if arg in (0, 1, 2):
117 117 self.shell.autocall = arg
118 118 else: # toggle
119 119 if self.shell.autocall:
120 120 self._magic_state.autocall_save = self.shell.autocall
121 121 self.shell.autocall = 0
122 122 else:
123 123 try:
124 124 self.shell.autocall = self._magic_state.autocall_save
125 125 except AttributeError:
126 126 self.shell.autocall = self._magic_state.autocall_save = 1
127 127
128 128 print "Automatic calling is:",['OFF','Smart','Full'][self.shell.autocall]
@@ -1,513 +1,513 b''
1 1 """Implementation of basic magic functions.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14 from __future__ import print_function
15 15
16 16 # Stdlib
17 17 import io
18 18 import sys
19 19 from pprint import pformat
20 20
21 21 # Our own packages
22 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 24 from IPython.core.prefilter import ESC_MAGIC
25 25 from IPython.utils.text import format_screen
26 26 from IPython.core import magic_arguments, page
27 27 from IPython.testing.skipdoctest import skip_doctest
28 28 from IPython.utils.ipstruct import Struct
29 29 from IPython.utils.path import unquote_filename
30 30 from IPython.utils.warn import warn, error
31 31
32 32 #-----------------------------------------------------------------------------
33 33 # Magics class implementation
34 34 #-----------------------------------------------------------------------------
35 35
36 @register_magics
36 @magics_class
37 37 class BasicMagics(Magics):
38 38 """Magics that provide central IPython functionality.
39 39
40 40 These are various magics that don't fit into specific categories but that
41 41 are all part of the base 'IPython experience'."""
42 42
43 43 def _lsmagic(self):
44 44 mesc = ESC_MAGIC
45 45 cesc = mesc*2
46 46 mman = self.shell.magics_manager
47 47 magics = mman.lsmagic()
48 48 out = ['Available line magics:',
49 49 mesc + (' '+mesc).join(magics['line']),
50 50 '',
51 51 'Available cell magics:',
52 52 cesc + (' '+cesc).join(magics['cell']),
53 53 '',
54 54 mman.auto_status()]
55 55 return '\n'.join(out)
56 56
57 57 @line_magic
58 58 def lsmagic(self, parameter_s=''):
59 59 """List currently available magic functions."""
60 60 print(self._lsmagic())
61 61
62 62 @line_magic
63 63 def magic(self, parameter_s=''):
64 64 """Print information about the magic function system.
65 65
66 66 Supported formats: -latex, -brief, -rest
67 67 """
68 68
69 69 mode = ''
70 70 try:
71 71 mode = parameter_s.split()[0][1:]
72 72 if mode == 'rest':
73 73 rest_docs = []
74 74 except:
75 75 pass
76 76
77 77 magic_docs = []
78 78 escapes = dict(line=ESC_MAGIC, cell=ESC_MAGIC*2)
79 79 magics = self.shell.magics_manager.magics
80 80
81 81 for mtype in ('line', 'cell'):
82 82 escape = escapes[mtype]
83 83 for fname, fn in magics[mtype].iteritems():
84 84
85 85 if mode == 'brief':
86 86 # only first line
87 87 if fn.__doc__:
88 88 fndoc = fn.__doc__.split('\n',1)[0]
89 89 else:
90 90 fndoc = 'No documentation'
91 91 else:
92 92 if fn.__doc__:
93 93 fndoc = fn.__doc__.rstrip()
94 94 else:
95 95 fndoc = 'No documentation'
96 96
97 97 if mode == 'rest':
98 98 rest_docs.append('**%s%s**::\n\n\t%s\n\n' %
99 99 (escape, fname, fndoc))
100 100 else:
101 101 magic_docs.append('%s%s:\n\t%s\n' %
102 102 (escape, fname, fndoc))
103 103
104 104 magic_docs = ''.join(magic_docs)
105 105
106 106 if mode == 'rest':
107 107 return "".join(rest_docs)
108 108
109 109 if mode == 'latex':
110 110 print(self.format_latex(magic_docs))
111 111 return
112 112 else:
113 113 magic_docs = format_screen(magic_docs)
114 114 if mode == 'brief':
115 115 return magic_docs
116 116
117 117 out = ["""
118 118 IPython's 'magic' functions
119 119 ===========================
120 120
121 121 The magic function system provides a series of functions which allow you to
122 122 control the behavior of IPython itself, plus a lot of system-type
123 123 features. All these functions are prefixed with a % character, but parameters
124 124 are given without parentheses or quotes.
125 125
126 126 NOTE: If you have 'automagic' enabled (via the command line option or with the
127 127 %automagic function), you don't need to type in the % explicitly. By default,
128 128 IPython ships with automagic on, so you should only rarely need the % escape.
129 129
130 130 Example: typing '%cd mydir' (without the quotes) changes you working directory
131 131 to 'mydir', if it exists.
132 132
133 133 For a list of the available magic functions, use %lsmagic. For a description
134 134 of any of them, type %magic_name?, e.g. '%cd?'.
135 135
136 136 Currently the magic system has the following functions:""",
137 137 magic_docs,
138 138 "Summary of magic functions (from %slsmagic):",
139 139 self._lsmagic(),
140 140 ]
141 141 page.page('\n'.join(out))
142 142
143 143
144 144 @line_magic
145 145 def page(self, parameter_s=''):
146 146 """Pretty print the object and display it through a pager.
147 147
148 148 %page [options] OBJECT
149 149
150 150 If no object is given, use _ (last output).
151 151
152 152 Options:
153 153
154 154 -r: page str(object), don't pretty-print it."""
155 155
156 156 # After a function contributed by Olivier Aubert, slightly modified.
157 157
158 158 # Process options/args
159 159 opts, args = self.parse_options(parameter_s, 'r')
160 160 raw = 'r' in opts
161 161
162 162 oname = args and args or '_'
163 163 info = self._ofind(oname)
164 164 if info['found']:
165 165 txt = (raw and str or pformat)( info['obj'] )
166 166 page.page(txt)
167 167 else:
168 168 print('Object `%s` not found' % oname)
169 169
170 170 @line_magic
171 171 def profile(self, parameter_s=''):
172 172 """Print your currently active IPython profile."""
173 173 from IPython.core.application import BaseIPythonApplication
174 174 if BaseIPythonApplication.initialized():
175 175 print(BaseIPythonApplication.instance().profile)
176 176 else:
177 177 error("profile is an application-level value, but you don't appear to be in an IPython application")
178 178
179 179 @line_magic
180 180 def pprint(self, parameter_s=''):
181 181 """Toggle pretty printing on/off."""
182 182 ptformatter = self.shell.display_formatter.formatters['text/plain']
183 183 ptformatter.pprint = bool(1 - ptformatter.pprint)
184 184 print('Pretty printing has been turned',
185 185 ['OFF','ON'][ptformatter.pprint])
186 186
187 187 @line_magic
188 188 def colors(self, parameter_s=''):
189 189 """Switch color scheme for prompts, info system and exception handlers.
190 190
191 191 Currently implemented schemes: NoColor, Linux, LightBG.
192 192
193 193 Color scheme names are not case-sensitive.
194 194
195 195 Examples
196 196 --------
197 197 To get a plain black and white terminal::
198 198
199 199 %colors nocolor
200 200 """
201 201 def color_switch_err(name):
202 202 warn('Error changing %s color schemes.\n%s' %
203 203 (name, sys.exc_info()[1]))
204 204
205 205
206 206 new_scheme = parameter_s.strip()
207 207 if not new_scheme:
208 208 raise UsageError(
209 209 "%colors: you must specify a color scheme. See '%colors?'")
210 210 return
211 211 # local shortcut
212 212 shell = self.shell
213 213
214 214 import IPython.utils.rlineimpl as readline
215 215
216 216 if not shell.colors_force and \
217 217 not readline.have_readline and sys.platform == "win32":
218 218 msg = """\
219 219 Proper color support under MS Windows requires the pyreadline library.
220 220 You can find it at:
221 221 http://ipython.org/pyreadline.html
222 222 Gary's readline needs the ctypes module, from:
223 223 http://starship.python.net/crew/theller/ctypes
224 224 (Note that ctypes is already part of Python versions 2.5 and newer).
225 225
226 226 Defaulting color scheme to 'NoColor'"""
227 227 new_scheme = 'NoColor'
228 228 warn(msg)
229 229
230 230 # readline option is 0
231 231 if not shell.colors_force and not shell.has_readline:
232 232 new_scheme = 'NoColor'
233 233
234 234 # Set prompt colors
235 235 try:
236 236 shell.prompt_manager.color_scheme = new_scheme
237 237 except:
238 238 color_switch_err('prompt')
239 239 else:
240 240 shell.colors = \
241 241 shell.prompt_manager.color_scheme_table.active_scheme_name
242 242 # Set exception colors
243 243 try:
244 244 shell.InteractiveTB.set_colors(scheme = new_scheme)
245 245 shell.SyntaxTB.set_colors(scheme = new_scheme)
246 246 except:
247 247 color_switch_err('exception')
248 248
249 249 # Set info (for 'object?') colors
250 250 if shell.color_info:
251 251 try:
252 252 shell.inspector.set_active_scheme(new_scheme)
253 253 except:
254 254 color_switch_err('object inspector')
255 255 else:
256 256 shell.inspector.set_active_scheme('NoColor')
257 257
258 258 @line_magic
259 259 def xmode(self, parameter_s=''):
260 260 """Switch modes for the exception handlers.
261 261
262 262 Valid modes: Plain, Context and Verbose.
263 263
264 264 If called without arguments, acts as a toggle."""
265 265
266 266 def xmode_switch_err(name):
267 267 warn('Error changing %s exception modes.\n%s' %
268 268 (name,sys.exc_info()[1]))
269 269
270 270 shell = self.shell
271 271 new_mode = parameter_s.strip().capitalize()
272 272 try:
273 273 shell.InteractiveTB.set_mode(mode=new_mode)
274 274 print('Exception reporting mode:',shell.InteractiveTB.mode)
275 275 except:
276 276 xmode_switch_err('user')
277 277
278 278 @line_magic
279 279 def quickref(self,arg):
280 280 """ Show a quick reference sheet """
281 281 from IPython.core.usage import quick_reference
282 282 qr = quick_reference + self.magic('-brief')
283 283 page.page(qr)
284 284
285 285 @line_magic
286 286 def doctest_mode(self, parameter_s=''):
287 287 """Toggle doctest mode on and off.
288 288
289 289 This mode is intended to make IPython behave as much as possible like a
290 290 plain Python shell, from the perspective of how its prompts, exceptions
291 291 and output look. This makes it easy to copy and paste parts of a
292 292 session into doctests. It does so by:
293 293
294 294 - Changing the prompts to the classic ``>>>`` ones.
295 295 - Changing the exception reporting mode to 'Plain'.
296 296 - Disabling pretty-printing of output.
297 297
298 298 Note that IPython also supports the pasting of code snippets that have
299 299 leading '>>>' and '...' prompts in them. This means that you can paste
300 300 doctests from files or docstrings (even if they have leading
301 301 whitespace), and the code will execute correctly. You can then use
302 302 '%history -t' to see the translated history; this will give you the
303 303 input after removal of all the leading prompts and whitespace, which
304 304 can be pasted back into an editor.
305 305
306 306 With these features, you can switch into this mode easily whenever you
307 307 need to do testing and changes to doctests, without having to leave
308 308 your existing IPython session.
309 309 """
310 310
311 311 # Shorthands
312 312 shell = self.shell
313 313 pm = shell.prompt_manager
314 314 meta = shell.meta
315 315 disp_formatter = self.shell.display_formatter
316 316 ptformatter = disp_formatter.formatters['text/plain']
317 317 # dstore is a data store kept in the instance metadata bag to track any
318 318 # changes we make, so we can undo them later.
319 319 dstore = meta.setdefault('doctest_mode',Struct())
320 320 save_dstore = dstore.setdefault
321 321
322 322 # save a few values we'll need to recover later
323 323 mode = save_dstore('mode',False)
324 324 save_dstore('rc_pprint',ptformatter.pprint)
325 325 save_dstore('xmode',shell.InteractiveTB.mode)
326 326 save_dstore('rc_separate_out',shell.separate_out)
327 327 save_dstore('rc_separate_out2',shell.separate_out2)
328 328 save_dstore('rc_prompts_pad_left',pm.justify)
329 329 save_dstore('rc_separate_in',shell.separate_in)
330 330 save_dstore('rc_plain_text_only',disp_formatter.plain_text_only)
331 331 save_dstore('prompt_templates',(pm.in_template, pm.in2_template, pm.out_template))
332 332
333 333 if mode == False:
334 334 # turn on
335 335 pm.in_template = '>>> '
336 336 pm.in2_template = '... '
337 337 pm.out_template = ''
338 338
339 339 # Prompt separators like plain python
340 340 shell.separate_in = ''
341 341 shell.separate_out = ''
342 342 shell.separate_out2 = ''
343 343
344 344 pm.justify = False
345 345
346 346 ptformatter.pprint = False
347 347 disp_formatter.plain_text_only = True
348 348
349 349 shell.magic('xmode Plain')
350 350 else:
351 351 # turn off
352 352 pm.in_template, pm.in2_template, pm.out_template = dstore.prompt_templates
353 353
354 354 shell.separate_in = dstore.rc_separate_in
355 355
356 356 shell.separate_out = dstore.rc_separate_out
357 357 shell.separate_out2 = dstore.rc_separate_out2
358 358
359 359 pm.justify = dstore.rc_prompts_pad_left
360 360
361 361 ptformatter.pprint = dstore.rc_pprint
362 362 disp_formatter.plain_text_only = dstore.rc_plain_text_only
363 363
364 364 shell.magic('xmode ' + dstore.xmode)
365 365
366 366 # Store new mode and inform
367 367 dstore.mode = bool(1-int(mode))
368 368 mode_label = ['OFF','ON'][dstore.mode]
369 369 print('Doctest mode is:', mode_label)
370 370
371 371 @line_magic
372 372 def gui(self, parameter_s=''):
373 373 """Enable or disable IPython GUI event loop integration.
374 374
375 375 %gui [GUINAME]
376 376
377 377 This magic replaces IPython's threaded shells that were activated
378 378 using the (pylab/wthread/etc.) command line flags. GUI toolkits
379 379 can now be enabled at runtime and keyboard
380 380 interrupts should work without any problems. The following toolkits
381 381 are supported: wxPython, PyQt4, PyGTK, Tk and Cocoa (OSX)::
382 382
383 383 %gui wx # enable wxPython event loop integration
384 384 %gui qt4|qt # enable PyQt4 event loop integration
385 385 %gui gtk # enable PyGTK event loop integration
386 386 %gui gtk3 # enable Gtk3 event loop integration
387 387 %gui tk # enable Tk event loop integration
388 388 %gui OSX # enable Cocoa event loop integration
389 389 # (requires %matplotlib 1.1)
390 390 %gui # disable all event loop integration
391 391
392 392 WARNING: after any of these has been called you can simply create
393 393 an application object, but DO NOT start the event loop yourself, as
394 394 we have already handled that.
395 395 """
396 396 opts, arg = self.parse_options(parameter_s, '')
397 397 if arg=='': arg = None
398 398 try:
399 399 return self.enable_gui(arg)
400 400 except Exception as e:
401 401 # print simple error message, rather than traceback if we can't
402 402 # hook up the GUI
403 403 error(str(e))
404 404
405 405 @skip_doctest
406 406 @line_magic
407 407 def precision(self, s=''):
408 408 """Set floating point precision for pretty printing.
409 409
410 410 Can set either integer precision or a format string.
411 411
412 412 If numpy has been imported and precision is an int,
413 413 numpy display precision will also be set, via ``numpy.set_printoptions``.
414 414
415 415 If no argument is given, defaults will be restored.
416 416
417 417 Examples
418 418 --------
419 419 ::
420 420
421 421 In [1]: from math import pi
422 422
423 423 In [2]: %precision 3
424 424 Out[2]: u'%.3f'
425 425
426 426 In [3]: pi
427 427 Out[3]: 3.142
428 428
429 429 In [4]: %precision %i
430 430 Out[4]: u'%i'
431 431
432 432 In [5]: pi
433 433 Out[5]: 3
434 434
435 435 In [6]: %precision %e
436 436 Out[6]: u'%e'
437 437
438 438 In [7]: pi**10
439 439 Out[7]: 9.364805e+04
440 440
441 441 In [8]: %precision
442 442 Out[8]: u'%r'
443 443
444 444 In [9]: pi**10
445 445 Out[9]: 93648.047476082982
446 446 """
447 447 ptformatter = self.shell.display_formatter.formatters['text/plain']
448 448 ptformatter.float_precision = s
449 449 return ptformatter.float_format
450 450
451 451 @magic_arguments.magic_arguments()
452 452 @magic_arguments.argument(
453 453 '-e', '--export', action='store_true', default=False,
454 454 help='Export IPython history as a notebook. The filename argument '
455 455 'is used to specify the notebook name and format. For example '
456 456 'a filename of notebook.ipynb will result in a notebook name '
457 457 'of "notebook" and a format of "xml". Likewise using a ".json" '
458 458 'or ".py" file extension will write the notebook in the json '
459 459 'or py formats.'
460 460 )
461 461 @magic_arguments.argument(
462 462 '-f', '--format',
463 463 help='Convert an existing IPython notebook to a new format. This option '
464 464 'specifies the new format and can have the values: xml, json, py. '
465 465 'The target filename is chosen automatically based on the new '
466 466 'format. The filename argument gives the name of the source file.'
467 467 )
468 468 @magic_arguments.argument(
469 469 'filename', type=unicode,
470 470 help='Notebook name or filename'
471 471 )
472 472 @line_magic
473 473 def notebook(self, s):
474 474 """Export and convert IPython notebooks.
475 475
476 476 This function can export the current IPython history to a notebook file
477 477 or can convert an existing notebook file into a different format. For
478 478 example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
479 479 To export the history to "foo.py" do "%notebook -e foo.py". To convert
480 480 "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
481 481 formats include (json/ipynb, py).
482 482 """
483 483 args = magic_arguments.parse_argstring(self.notebook, s)
484 484
485 485 from IPython.nbformat import current
486 486 args.filename = unquote_filename(args.filename)
487 487 if args.export:
488 488 fname, name, format = current.parse_filename(args.filename)
489 489 cells = []
490 490 hist = list(self.shell.history_manager.get_range())
491 491 for session, prompt_number, input in hist[:-1]:
492 492 cells.append(current.new_code_cell(prompt_number=prompt_number,
493 493 input=input))
494 494 worksheet = current.new_worksheet(cells=cells)
495 495 nb = current.new_notebook(name=name,worksheets=[worksheet])
496 496 with io.open(fname, 'w', encoding='utf-8') as f:
497 497 current.write(nb, f, format);
498 498 elif args.format is not None:
499 499 old_fname, old_name, old_format = current.parse_filename(args.filename)
500 500 new_format = args.format
501 501 if new_format == u'xml':
502 502 raise ValueError('Notebooks cannot be written as xml.')
503 503 elif new_format == u'ipynb' or new_format == u'json':
504 504 new_fname = old_name + u'.ipynb'
505 505 new_format = u'json'
506 506 elif new_format == u'py':
507 507 new_fname = old_name + u'.py'
508 508 else:
509 509 raise ValueError('Invalid notebook format: %s' % new_format)
510 510 with io.open(old_fname, 'r', encoding='utf-8') as f:
511 511 nb = current.read(f, old_format)
512 512 with io.open(new_fname, 'w', encoding='utf-8') as f:
513 513 current.write(nb, f, new_format)
@@ -1,478 +1,478 b''
1 1 """Implementation of code management magic functions.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 # Stdlib
16 16 import inspect
17 17 import io
18 18 import json
19 19 import os
20 20 import sys
21 21 from urllib2 import urlopen
22 22
23 23 # Our own packages
24 24 from IPython.core.error import TryNext
25 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 27 from IPython.testing.skipdoctest import skip_doctest
28 28 from IPython.utils import openpy
29 29 from IPython.utils import py3compat
30 30 from IPython.utils.io import file_read
31 31 from IPython.utils.path import get_py_filename, unquote_filename
32 32 from IPython.utils.warn import warn
33 33
34 34 #-----------------------------------------------------------------------------
35 35 # Magic implementation classes
36 36 #-----------------------------------------------------------------------------
37 37
38 38 # Used for exception handling in magic_edit
39 39 class MacroToEdit(ValueError): pass
40 40
41 41
42 @register_magics
42 @magics_class
43 43 class CodeMagics(Magics):
44 44 """Magics related to code management (loading, saving, editing, ...)."""
45 45
46 46 @line_magic
47 47 def save(self, parameter_s=''):
48 48 """Save a set of lines or a macro to a given filename.
49 49
50 50 Usage:\\
51 51 %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...
52 52
53 53 Options:
54 54
55 55 -r: use 'raw' input. By default, the 'processed' history is used,
56 56 so that magics are loaded in their transformed version to valid
57 57 Python. If this option is given, the raw input as typed as the
58 58 command line is used instead.
59 59
60 60 This function uses the same syntax as %history for input ranges,
61 61 then saves the lines to the filename you specify.
62 62
63 63 It adds a '.py' extension to the file if you don't do so yourself, and
64 64 it asks for confirmation before overwriting existing files."""
65 65
66 66 opts,args = self.parse_options(parameter_s,'r',mode='list')
67 67 fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
68 68 if not fname.endswith('.py'):
69 69 fname += '.py'
70 70 if os.path.isfile(fname):
71 71 ans = raw_input('File `%s` exists. Overwrite (y/[N])? ' % fname)
72 72 if ans.lower() not in ['y','yes']:
73 73 print 'Operation cancelled.'
74 74 return
75 75 try:
76 76 cmds = self.shell.find_user_code(codefrom, 'r' in opts)
77 77 except (TypeError, ValueError) as e:
78 78 print e.args[0]
79 79 return
80 80 with io.open(fname,'w', encoding="utf-8") as f:
81 81 f.write(u"# coding: utf-8\n")
82 82 f.write(py3compat.cast_unicode(cmds))
83 83 print 'The following commands were written to file `%s`:' % fname
84 84 print cmds
85 85
86 86 @line_magic
87 87 def pastebin(self, parameter_s=''):
88 88 """Upload code to Github's Gist paste bin, returning the URL.
89 89
90 90 Usage:\\
91 91 %pastebin [-d "Custom description"] 1-7
92 92
93 93 The argument can be an input history range, a filename, or the name of a
94 94 string or macro.
95 95
96 96 Options:
97 97
98 98 -d: Pass a custom description for the gist. The default will say
99 99 "Pasted from IPython".
100 100 """
101 101 opts, args = self.parse_options(parameter_s, 'd:')
102 102
103 103 try:
104 104 code = self.shell.find_user_code(args)
105 105 except (ValueError, TypeError) as e:
106 106 print e.args[0]
107 107 return
108 108
109 109 post_data = json.dumps({
110 110 "description": opts.get('d', "Pasted from IPython"),
111 111 "public": True,
112 112 "files": {
113 113 "file1.py": {
114 114 "content": code
115 115 }
116 116 }
117 117 }).encode('utf-8')
118 118
119 119 response = urlopen("https://api.github.com/gists", post_data)
120 120 response_data = json.loads(response.read().decode('utf-8'))
121 121 return response_data['html_url']
122 122
123 123 @line_magic
124 124 def loadpy(self, arg_s):
125 125 """Load a .py python script into the GUI console.
126 126
127 127 This magic command can either take a local filename or a url::
128 128
129 129 %loadpy myscript.py
130 130 %loadpy http://www.example.com/myscript.py
131 131 """
132 132 arg_s = unquote_filename(arg_s)
133 133 remote_url = arg_s.startswith(('http://', 'https://'))
134 134 local_url = not remote_url
135 135 if local_url and not arg_s.endswith('.py'):
136 136 # Local files must be .py; for remote URLs it's possible that the
137 137 # fetch URL doesn't have a .py in it (many servers have an opaque
138 138 # URL, such as scipy-central.org).
139 139 raise ValueError('%%loadpy only works with .py files: %s' % arg_s)
140 140
141 141 # openpy takes care of finding the source encoding (per PEP 263)
142 142 if remote_url:
143 143 contents = openpy.read_py_url(arg_s, skip_encoding_cookie=True)
144 144 else:
145 145 contents = openpy.read_py_file(arg_s, skip_encoding_cookie=True)
146 146
147 147 self.shell.set_next_input(contents)
148 148
149 149 def _find_edit_target(self, args, opts, last_call):
150 150 """Utility method used by magic_edit to find what to edit."""
151 151
152 152 def make_filename(arg):
153 153 "Make a filename from the given args"
154 154 arg = unquote_filename(arg)
155 155 try:
156 156 filename = get_py_filename(arg)
157 157 except IOError:
158 158 # If it ends with .py but doesn't already exist, assume we want
159 159 # a new file.
160 160 if arg.endswith('.py'):
161 161 filename = arg
162 162 else:
163 163 filename = None
164 164 return filename
165 165
166 166 # Set a few locals from the options for convenience:
167 167 opts_prev = 'p' in opts
168 168 opts_raw = 'r' in opts
169 169
170 170 # custom exceptions
171 171 class DataIsObject(Exception): pass
172 172
173 173 # Default line number value
174 174 lineno = opts.get('n',None)
175 175
176 176 if opts_prev:
177 177 args = '_%s' % last_call[0]
178 178 if not self.shell.user_ns.has_key(args):
179 179 args = last_call[1]
180 180
181 181 # use last_call to remember the state of the previous call, but don't
182 182 # let it be clobbered by successive '-p' calls.
183 183 try:
184 184 last_call[0] = self.shell.displayhook.prompt_count
185 185 if not opts_prev:
186 186 last_call[1] = args
187 187 except:
188 188 pass
189 189
190 190 # by default this is done with temp files, except when the given
191 191 # arg is a filename
192 192 use_temp = True
193 193
194 194 data = ''
195 195
196 196 # First, see if the arguments should be a filename.
197 197 filename = make_filename(args)
198 198 if filename:
199 199 use_temp = False
200 200 elif args:
201 201 # Mode where user specifies ranges of lines, like in %macro.
202 202 data = self.shell.extract_input_lines(args, opts_raw)
203 203 if not data:
204 204 try:
205 205 # Load the parameter given as a variable. If not a string,
206 206 # process it as an object instead (below)
207 207
208 208 #print '*** args',args,'type',type(args) # dbg
209 209 data = eval(args, self.shell.user_ns)
210 210 if not isinstance(data, basestring):
211 211 raise DataIsObject
212 212
213 213 except (NameError,SyntaxError):
214 214 # given argument is not a variable, try as a filename
215 215 filename = make_filename(args)
216 216 if filename is None:
217 217 warn("Argument given (%s) can't be found as a variable "
218 218 "or as a filename." % args)
219 219 return
220 220 use_temp = False
221 221
222 222 except DataIsObject:
223 223 # macros have a special edit function
224 224 if isinstance(data, Macro):
225 225 raise MacroToEdit(data)
226 226
227 227 # For objects, try to edit the file where they are defined
228 228 try:
229 229 filename = inspect.getabsfile(data)
230 230 if 'fakemodule' in filename.lower() and \
231 231 inspect.isclass(data):
232 232 # class created by %edit? Try to find source
233 233 # by looking for method definitions instead, the
234 234 # __module__ in those classes is FakeModule.
235 235 attrs = [getattr(data, aname) for aname in dir(data)]
236 236 for attr in attrs:
237 237 if not inspect.ismethod(attr):
238 238 continue
239 239 filename = inspect.getabsfile(attr)
240 240 if filename and \
241 241 'fakemodule' not in filename.lower():
242 242 # change the attribute to be the edit
243 243 # target instead
244 244 data = attr
245 245 break
246 246
247 247 datafile = 1
248 248 except TypeError:
249 249 filename = make_filename(args)
250 250 datafile = 1
251 251 warn('Could not find file where `%s` is defined.\n'
252 252 'Opening a file named `%s`' % (args,filename))
253 253 # Now, make sure we can actually read the source (if it was
254 254 # in a temp file it's gone by now).
255 255 if datafile:
256 256 try:
257 257 if lineno is None:
258 258 lineno = inspect.getsourcelines(data)[1]
259 259 except IOError:
260 260 filename = make_filename(args)
261 261 if filename is None:
262 262 warn('The file `%s` where `%s` was defined cannot '
263 263 'be read.' % (filename,data))
264 264 return
265 265 use_temp = False
266 266
267 267 if use_temp:
268 268 filename = self.shell.mktempfile(data)
269 269 print 'IPython will make a temporary file named:',filename
270 270
271 271 return filename, lineno, use_temp
272 272
273 273 def _edit_macro(self,mname,macro):
274 274 """open an editor with the macro data in a file"""
275 275 filename = self.shell.mktempfile(macro.value)
276 276 self.shell.hooks.editor(filename)
277 277
278 278 # and make a new macro object, to replace the old one
279 279 mfile = open(filename)
280 280 mvalue = mfile.read()
281 281 mfile.close()
282 282 self.shell.user_ns[mname] = Macro(mvalue)
283 283
284 284 @line_magic
285 285 def ed(self, parameter_s=''):
286 286 """Alias to %edit."""
287 287 return self.edit(parameter_s)
288 288
289 289 @skip_doctest
290 290 @line_magic
291 291 def edit(self, parameter_s='',last_call=['','']):
292 292 """Bring up an editor and execute the resulting code.
293 293
294 294 Usage:
295 295 %edit [options] [args]
296 296
297 297 %edit runs IPython's editor hook. The default version of this hook is
298 298 set to call the editor specified by your $EDITOR environment variable.
299 299 If this isn't found, it will default to vi under Linux/Unix and to
300 300 notepad under Windows. See the end of this docstring for how to change
301 301 the editor hook.
302 302
303 303 You can also set the value of this editor via the
304 304 ``TerminalInteractiveShell.editor`` option in your configuration file.
305 305 This is useful if you wish to use a different editor from your typical
306 306 default with IPython (and for Windows users who typically don't set
307 307 environment variables).
308 308
309 309 This command allows you to conveniently edit multi-line code right in
310 310 your IPython session.
311 311
312 312 If called without arguments, %edit opens up an empty editor with a
313 313 temporary file and will execute the contents of this file when you
314 314 close it (don't forget to save it!).
315 315
316 316
317 317 Options:
318 318
319 319 -n <number>: open the editor at a specified line number. By default,
320 320 the IPython editor hook uses the unix syntax 'editor +N filename', but
321 321 you can configure this by providing your own modified hook if your
322 322 favorite editor supports line-number specifications with a different
323 323 syntax.
324 324
325 325 -p: this will call the editor with the same data as the previous time
326 326 it was used, regardless of how long ago (in your current session) it
327 327 was.
328 328
329 329 -r: use 'raw' input. This option only applies to input taken from the
330 330 user's history. By default, the 'processed' history is used, so that
331 331 magics are loaded in their transformed version to valid Python. If
332 332 this option is given, the raw input as typed as the command line is
333 333 used instead. When you exit the editor, it will be executed by
334 334 IPython's own processor.
335 335
336 336 -x: do not execute the edited code immediately upon exit. This is
337 337 mainly useful if you are editing programs which need to be called with
338 338 command line arguments, which you can then do using %run.
339 339
340 340
341 341 Arguments:
342 342
343 343 If arguments are given, the following possibilities exist:
344 344
345 345 - If the argument is a filename, IPython will load that into the
346 346 editor. It will execute its contents with execfile() when you exit,
347 347 loading any code in the file into your interactive namespace.
348 348
349 349 - The arguments are ranges of input history, e.g. "7 ~1/4-6".
350 350 The syntax is the same as in the %history magic.
351 351
352 352 - If the argument is a string variable, its contents are loaded
353 353 into the editor. You can thus edit any string which contains
354 354 python code (including the result of previous edits).
355 355
356 356 - If the argument is the name of an object (other than a string),
357 357 IPython will try to locate the file where it was defined and open the
358 358 editor at the point where it is defined. You can use `%edit function`
359 359 to load an editor exactly at the point where 'function' is defined,
360 360 edit it and have the file be executed automatically.
361 361
362 362 - If the object is a macro (see %macro for details), this opens up your
363 363 specified editor with a temporary file containing the macro's data.
364 364 Upon exit, the macro is reloaded with the contents of the file.
365 365
366 366 Note: opening at an exact line is only supported under Unix, and some
367 367 editors (like kedit and gedit up to Gnome 2.8) do not understand the
368 368 '+NUMBER' parameter necessary for this feature. Good editors like
369 369 (X)Emacs, vi, jed, pico and joe all do.
370 370
371 371 After executing your code, %edit will return as output the code you
372 372 typed in the editor (except when it was an existing file). This way
373 373 you can reload the code in further invocations of %edit as a variable,
374 374 via _<NUMBER> or Out[<NUMBER>], where <NUMBER> is the prompt number of
375 375 the output.
376 376
377 377 Note that %edit is also available through the alias %ed.
378 378
379 379 This is an example of creating a simple function inside the editor and
380 380 then modifying it. First, start up the editor::
381 381
382 382 In [1]: ed
383 383 Editing... done. Executing edited code...
384 384 Out[1]: 'def foo():\\n print "foo() was defined in an editing
385 385 session"\\n'
386 386
387 387 We can then call the function foo()::
388 388
389 389 In [2]: foo()
390 390 foo() was defined in an editing session
391 391
392 392 Now we edit foo. IPython automatically loads the editor with the
393 393 (temporary) file where foo() was previously defined::
394 394
395 395 In [3]: ed foo
396 396 Editing... done. Executing edited code...
397 397
398 398 And if we call foo() again we get the modified version::
399 399
400 400 In [4]: foo()
401 401 foo() has now been changed!
402 402
403 403 Here is an example of how to edit a code snippet successive
404 404 times. First we call the editor::
405 405
406 406 In [5]: ed
407 407 Editing... done. Executing edited code...
408 408 hello
409 409 Out[5]: "print 'hello'\\n"
410 410
411 411 Now we call it again with the previous output (stored in _)::
412 412
413 413 In [6]: ed _
414 414 Editing... done. Executing edited code...
415 415 hello world
416 416 Out[6]: "print 'hello world'\\n"
417 417
418 418 Now we call it with the output #8 (stored in _8, also as Out[8])::
419 419
420 420 In [7]: ed _8
421 421 Editing... done. Executing edited code...
422 422 hello again
423 423 Out[7]: "print 'hello again'\\n"
424 424
425 425
426 426 Changing the default editor hook:
427 427
428 428 If you wish to write your own editor hook, you can put it in a
429 429 configuration file which you load at startup time. The default hook
430 430 is defined in the IPython.core.hooks module, and you can use that as a
431 431 starting example for further modifications. That file also has
432 432 general instructions on how to set a new hook for use once you've
433 433 defined it."""
434 434 opts,args = self.parse_options(parameter_s,'prxn:')
435 435
436 436 try:
437 437 filename, lineno, is_temp = self._find_edit_target(args, opts, last_call)
438 438 except MacroToEdit as e:
439 439 self._edit_macro(args, e.args[0])
440 440 return
441 441
442 442 # do actual editing here
443 443 print 'Editing...',
444 444 sys.stdout.flush()
445 445 try:
446 446 # Quote filenames that may have spaces in them
447 447 if ' ' in filename:
448 448 filename = "'%s'" % filename
449 449 self.shell.hooks.editor(filename,lineno)
450 450 except TryNext:
451 451 warn('Could not open editor')
452 452 return
453 453
454 454 # XXX TODO: should this be generalized for all string vars?
455 455 # For now, this is special-cased to blocks created by cpaste
456 456 if args.strip() == 'pasted_block':
457 457 self.shell.user_ns['pasted_block'] = file_read(filename)
458 458
459 459 if 'x' in opts: # -x prevents actual execution
460 460 print
461 461 else:
462 462 print 'done. Executing edited code...'
463 463 if 'r' in opts: # Untranslated IPython code
464 464 self.shell.run_cell(file_read(filename),
465 465 store_history=False)
466 466 else:
467 467 self.shell.safe_execfile(filename, self.shell.user_ns,
468 468 self.shell.user_ns)
469 469
470 470 if is_temp:
471 471 try:
472 472 return open(filename).read()
473 473 except IOError,msg:
474 474 if msg.filename == filename:
475 475 warn('File not found. Did you forget to save?')
476 476 return
477 477 else:
478 478 self.shell.showtraceback()
@@ -1,146 +1,146 b''
1 1 """Implementation of configuration-related magic functions.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 # Stdlib
16 16 import re
17 17
18 18 # Our own packages
19 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 21 from IPython.utils.warn import error
22 22
23 23 #-----------------------------------------------------------------------------
24 24 # Magic implementation classes
25 25 #-----------------------------------------------------------------------------
26 26
27 @register_magics
27 @magics_class
28 28 class ConfigMagics(Magics):
29 29
30 30 def __init__(self, shell):
31 31 super(ConfigMagics, self).__init__(shell)
32 32 self.configurables = []
33 33
34 34 @line_magic
35 35 def config(self, s):
36 36 """configure IPython
37 37
38 38 %config Class[.trait=value]
39 39
40 40 This magic exposes most of the IPython config system. Any
41 41 Configurable class should be able to be configured with the simple
42 42 line::
43 43
44 44 %config Class.trait=value
45 45
46 46 Where `value` will be resolved in the user's namespace, if it is an
47 47 expression or variable name.
48 48
49 49 Examples
50 50 --------
51 51
52 52 To see what classes are available for config, pass no arguments::
53 53
54 54 In [1]: %config
55 55 Available objects for config:
56 56 TerminalInteractiveShell
57 57 HistoryManager
58 58 PrefilterManager
59 59 AliasManager
60 60 IPCompleter
61 61 PromptManager
62 62 DisplayFormatter
63 63
64 64 To view what is configurable on a given class, just pass the class
65 65 name::
66 66
67 67 In [2]: %config IPCompleter
68 68 IPCompleter options
69 69 -----------------
70 70 IPCompleter.omit__names=<Enum>
71 71 Current: 2
72 72 Choices: (0, 1, 2)
73 73 Instruct the completer to omit private method names
74 74 Specifically, when completing on ``object.<tab>``.
75 75 When 2 [default]: all names that start with '_' will be excluded.
76 76 When 1: all 'magic' names (``__foo__``) will be excluded.
77 77 When 0: nothing will be excluded.
78 78 IPCompleter.merge_completions=<CBool>
79 79 Current: True
80 80 Whether to merge completion results into a single list
81 81 If False, only the completion results from the first non-empty
82 82 completer will be returned.
83 83 IPCompleter.limit_to__all__=<CBool>
84 84 Current: False
85 85 Instruct the completer to use __all__ for the completion
86 86 Specifically, when completing on ``object.<tab>``.
87 87 When True: only those names in obj.__all__ will be included.
88 88 When False [default]: the __all__ attribute is ignored
89 89 IPCompleter.greedy=<CBool>
90 90 Current: False
91 91 Activate greedy completion
92 92 This will enable completion on elements of lists, results of
93 93 function calls, etc., but can be unsafe because the code is
94 94 actually evaluated on TAB.
95 95
96 96 but the real use is in setting values::
97 97
98 98 In [3]: %config IPCompleter.greedy = True
99 99
100 100 and these values are read from the user_ns if they are variables::
101 101
102 102 In [4]: feeling_greedy=False
103 103
104 104 In [5]: %config IPCompleter.greedy = feeling_greedy
105 105
106 106 """
107 107 from IPython.config.loader import Config
108 108 # some IPython objects are Configurable, but do not yet have
109 109 # any configurable traits. Exclude them from the effects of
110 110 # this magic, as their presence is just noise:
111 111 configurables = [ c for c in self.shell.configurables
112 112 if c.__class__.class_traits(config=True) ]
113 113 classnames = [ c.__class__.__name__ for c in configurables ]
114 114
115 115 line = s.strip()
116 116 if not line:
117 117 # print available configurable names
118 118 print "Available objects for config:"
119 119 for name in classnames:
120 120 print " ", name
121 121 return
122 122 elif line in classnames:
123 123 # `%config TerminalInteractiveShell` will print trait info for
124 124 # TerminalInteractiveShell
125 125 c = configurables[classnames.index(line)]
126 126 cls = c.__class__
127 127 help = cls.class_get_help(c)
128 128 # strip leading '--' from cl-args:
129 129 help = re.sub(re.compile(r'^--', re.MULTILINE), '', help)
130 130 print help
131 131 return
132 132 elif '=' not in line:
133 133 raise UsageError("Invalid config statement: %r, "
134 134 "should be Class.trait = value" % line)
135 135
136 136 # otherwise, assume we are setting configurables.
137 137 # leave quotes on args when splitting, because we want
138 138 # unquoted args to eval in user_ns
139 139 cfg = Config()
140 140 exec "cfg."+line in locals(), self.shell.user_ns
141 141
142 142 for configurable in configurables:
143 143 try:
144 144 configurable.update_config(cfg)
145 145 except Exception as e:
146 146 error(e)
@@ -1,45 +1,45 b''
1 1 """Deprecated Magic functions.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 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 19 # Magic implementation classes
20 20 #-----------------------------------------------------------------------------
21 21
22 @register_magics
22 @magics_class
23 23 class DeprecatedMagics(Magics):
24 24 """Magics slated for later removal."""
25 25
26 26 @line_magic
27 27 def install_profiles(self, parameter_s=''):
28 28 """%install_profiles has been deprecated."""
29 29 print '\n'.join([
30 30 "%install_profiles has been deprecated.",
31 31 "Use `ipython profile list` to view available profiles.",
32 32 "Requesting a profile with `ipython profile create <name>`",
33 33 "or `ipython --profile=<name>` will start with the bundled",
34 34 "profile of that name if it exists."
35 35 ])
36 36
37 37 @line_magic
38 38 def install_default_config(self, parameter_s=''):
39 39 """%install_default_config has been deprecated."""
40 40 print '\n'.join([
41 41 "%install_default_config has been deprecated.",
42 42 "Use `ipython profile create <name>` to initialize a profile",
43 43 "with the default config files.",
44 44 "Add `--reset` to overwrite already existing config files with defaults."
45 45 ])
@@ -1,955 +1,955 b''
1 1 """Implementation of execution-related magic functions.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 # Stdlib
16 16 import __builtin__ as builtin_mod
17 17 import bdb
18 18 import os
19 19 import sys
20 20 import time
21 21 from StringIO import StringIO
22 22
23 23 # cProfile was added in Python2.5
24 24 try:
25 25 import cProfile as profile
26 26 import pstats
27 27 except ImportError:
28 28 # profile isn't bundled by default in Debian for license reasons
29 29 try:
30 30 import profile, pstats
31 31 except ImportError:
32 32 profile = pstats = None
33 33
34 34 # Our own packages
35 35 from IPython.core import debugger, oinspect
36 36 from IPython.core import page
37 37 from IPython.core.error import UsageError
38 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 40 on_off, needs_local_scope)
41 41 from IPython.testing.skipdoctest import skip_doctest
42 42 from IPython.utils import py3compat
43 43 from IPython.utils.ipstruct import Struct
44 44 from IPython.utils.module_paths import find_mod
45 45 from IPython.utils.path import get_py_filename, unquote_filename
46 46 from IPython.utils.timing import clock, clock2
47 47 from IPython.utils.warn import warn, error
48 48
49 49 #-----------------------------------------------------------------------------
50 50 # Magic implementation classes
51 51 #-----------------------------------------------------------------------------
52 52
53 @register_magics
53 @magics_class
54 54 class ExecutionMagics(Magics):
55 55 """Magics related to code execution, debugging, profiling, etc.
56 56
57 57 """
58 58
59 59 def __init__(self, shell):
60 60 super(ExecutionMagics, self).__init__(shell)
61 61 if profile is None:
62 62 self.prun = self.profile_missing_notice
63 63 # Default execution function used to actually run user code.
64 64 self.default_runner = None
65 65
66 66 def profile_missing_notice(self, *args, **kwargs):
67 67 error("""\
68 68 The profile module could not be found. It has been removed from the standard
69 69 python packages because of its non-free license. To use profiling, install the
70 70 python-profiler package from non-free.""")
71 71
72 72 @skip_doctest
73 73 @line_magic
74 74 def prun(self, parameter_s='',user_mode=1,
75 75 opts=None,arg_lst=None,prog_ns=None):
76 76
77 77 """Run a statement through the python code profiler.
78 78
79 79 Usage:
80 80 %prun [options] statement
81 81
82 82 The given statement (which doesn't require quote marks) is run via the
83 83 python profiler in a manner similar to the profile.run() function.
84 84 Namespaces are internally managed to work correctly; profile.run
85 85 cannot be used in IPython because it makes certain assumptions about
86 86 namespaces which do not hold under IPython.
87 87
88 88 Options:
89 89
90 90 -l <limit>: you can place restrictions on what or how much of the
91 91 profile gets printed. The limit value can be:
92 92
93 93 * A string: only information for function names containing this string
94 94 is printed.
95 95
96 96 * An integer: only these many lines are printed.
97 97
98 98 * A float (between 0 and 1): this fraction of the report is printed
99 99 (for example, use a limit of 0.4 to see the topmost 40% only).
100 100
101 101 You can combine several limits with repeated use of the option. For
102 102 example, '-l __init__ -l 5' will print only the topmost 5 lines of
103 103 information about class constructors.
104 104
105 105 -r: return the pstats.Stats object generated by the profiling. This
106 106 object has all the information about the profile in it, and you can
107 107 later use it for further analysis or in other functions.
108 108
109 109 -s <key>: sort profile by given key. You can provide more than one key
110 110 by using the option several times: '-s key1 -s key2 -s key3...'. The
111 111 default sorting key is 'time'.
112 112
113 113 The following is copied verbatim from the profile documentation
114 114 referenced below:
115 115
116 116 When more than one key is provided, additional keys are used as
117 117 secondary criteria when the there is equality in all keys selected
118 118 before them.
119 119
120 120 Abbreviations can be used for any key names, as long as the
121 121 abbreviation is unambiguous. The following are the keys currently
122 122 defined:
123 123
124 124 Valid Arg Meaning
125 125 "calls" call count
126 126 "cumulative" cumulative time
127 127 "file" file name
128 128 "module" file name
129 129 "pcalls" primitive call count
130 130 "line" line number
131 131 "name" function name
132 132 "nfl" name/file/line
133 133 "stdname" standard name
134 134 "time" internal time
135 135
136 136 Note that all sorts on statistics are in descending order (placing
137 137 most time consuming items first), where as name, file, and line number
138 138 searches are in ascending order (i.e., alphabetical). The subtle
139 139 distinction between "nfl" and "stdname" is that the standard name is a
140 140 sort of the name as printed, which means that the embedded line
141 141 numbers get compared in an odd way. For example, lines 3, 20, and 40
142 142 would (if the file names were the same) appear in the string order
143 143 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
144 144 line numbers. In fact, sort_stats("nfl") is the same as
145 145 sort_stats("name", "file", "line").
146 146
147 147 -T <filename>: save profile results as shown on screen to a text
148 148 file. The profile is still shown on screen.
149 149
150 150 -D <filename>: save (via dump_stats) profile statistics to given
151 151 filename. This data is in a format understood by the pstats module, and
152 152 is generated by a call to the dump_stats() method of profile
153 153 objects. The profile is still shown on screen.
154 154
155 155 -q: suppress output to the pager. Best used with -T and/or -D above.
156 156
157 157 If you want to run complete programs under the profiler's control, use
158 158 '%run -p [prof_opts] filename.py [args to program]' where prof_opts
159 159 contains profiler specific options as described here.
160 160
161 161 You can read the complete documentation for the profile module with::
162 162
163 163 In [1]: import profile; profile.help()
164 164 """
165 165
166 166 opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
167 167
168 168 if user_mode: # regular user call
169 169 opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q',
170 170 list_all=1, posix=False)
171 171 namespace = self.shell.user_ns
172 172 else: # called to run a program by %run -p
173 173 try:
174 174 filename = get_py_filename(arg_lst[0])
175 175 except IOError as e:
176 176 try:
177 177 msg = str(e)
178 178 except UnicodeError:
179 179 msg = e.message
180 180 error(msg)
181 181 return
182 182
183 183 arg_str = 'execfile(filename,prog_ns)'
184 184 namespace = {
185 185 'execfile': self.shell.safe_execfile,
186 186 'prog_ns': prog_ns,
187 187 'filename': filename
188 188 }
189 189
190 190 opts.merge(opts_def)
191 191
192 192 prof = profile.Profile()
193 193 try:
194 194 prof = prof.runctx(arg_str,namespace,namespace)
195 195 sys_exit = ''
196 196 except SystemExit:
197 197 sys_exit = """*** SystemExit exception caught in code being profiled."""
198 198
199 199 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
200 200
201 201 lims = opts.l
202 202 if lims:
203 203 lims = [] # rebuild lims with ints/floats/strings
204 204 for lim in opts.l:
205 205 try:
206 206 lims.append(int(lim))
207 207 except ValueError:
208 208 try:
209 209 lims.append(float(lim))
210 210 except ValueError:
211 211 lims.append(lim)
212 212
213 213 # Trap output.
214 214 stdout_trap = StringIO()
215 215
216 216 if hasattr(stats,'stream'):
217 217 # In newer versions of python, the stats object has a 'stream'
218 218 # attribute to write into.
219 219 stats.stream = stdout_trap
220 220 stats.print_stats(*lims)
221 221 else:
222 222 # For older versions, we manually redirect stdout during printing
223 223 sys_stdout = sys.stdout
224 224 try:
225 225 sys.stdout = stdout_trap
226 226 stats.print_stats(*lims)
227 227 finally:
228 228 sys.stdout = sys_stdout
229 229
230 230 output = stdout_trap.getvalue()
231 231 output = output.rstrip()
232 232
233 233 if 'q' not in opts:
234 234 page.page(output)
235 235 print sys_exit,
236 236
237 237 dump_file = opts.D[0]
238 238 text_file = opts.T[0]
239 239 if dump_file:
240 240 dump_file = unquote_filename(dump_file)
241 241 prof.dump_stats(dump_file)
242 242 print '\n*** Profile stats marshalled to file',\
243 243 `dump_file`+'.',sys_exit
244 244 if text_file:
245 245 text_file = unquote_filename(text_file)
246 246 pfile = open(text_file,'w')
247 247 pfile.write(output)
248 248 pfile.close()
249 249 print '\n*** Profile printout saved to text file',\
250 250 `text_file`+'.',sys_exit
251 251
252 252 if opts.has_key('r'):
253 253 return stats
254 254 else:
255 255 return None
256 256
257 257 @line_magic
258 258 def pdb(self, parameter_s=''):
259 259 """Control the automatic calling of the pdb interactive debugger.
260 260
261 261 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
262 262 argument it works as a toggle.
263 263
264 264 When an exception is triggered, IPython can optionally call the
265 265 interactive pdb debugger after the traceback printout. %pdb toggles
266 266 this feature on and off.
267 267
268 268 The initial state of this feature is set in your configuration
269 269 file (the option is ``InteractiveShell.pdb``).
270 270
271 271 If you want to just activate the debugger AFTER an exception has fired,
272 272 without having to type '%pdb on' and rerunning your code, you can use
273 273 the %debug magic."""
274 274
275 275 par = parameter_s.strip().lower()
276 276
277 277 if par:
278 278 try:
279 279 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
280 280 except KeyError:
281 281 print ('Incorrect argument. Use on/1, off/0, '
282 282 'or nothing for a toggle.')
283 283 return
284 284 else:
285 285 # toggle
286 286 new_pdb = not self.shell.call_pdb
287 287
288 288 # set on the shell
289 289 self.shell.call_pdb = new_pdb
290 290 print 'Automatic pdb calling has been turned',on_off(new_pdb)
291 291
292 292 @line_magic
293 293 def debug(self, parameter_s=''):
294 294 """Activate the interactive debugger in post-mortem mode.
295 295
296 296 If an exception has just occurred, this lets you inspect its stack
297 297 frames interactively. Note that this will always work only on the last
298 298 traceback that occurred, so you must call this quickly after an
299 299 exception that you wish to inspect has fired, because if another one
300 300 occurs, it clobbers the previous one.
301 301
302 302 If you want IPython to automatically do this on every exception, see
303 303 the %pdb magic for more details.
304 304 """
305 305 self.shell.debugger(force=True)
306 306
307 307 @line_magic
308 308 def tb(self, s):
309 309 """Print the last traceback with the currently active exception mode.
310 310
311 311 See %xmode for changing exception reporting modes."""
312 312 self.shell.showtraceback()
313 313
314 314 @skip_doctest
315 315 @line_magic
316 316 def run(self, parameter_s='', runner=None,
317 317 file_finder=get_py_filename):
318 318 """Run the named file inside IPython as a program.
319 319
320 320 Usage:\\
321 321 %run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]
322 322
323 323 Parameters after the filename are passed as command-line arguments to
324 324 the program (put in sys.argv). Then, control returns to IPython's
325 325 prompt.
326 326
327 327 This is similar to running at a system prompt:\\
328 328 $ python file args\\
329 329 but with the advantage of giving you IPython's tracebacks, and of
330 330 loading all variables into your interactive namespace for further use
331 331 (unless -p is used, see below).
332 332
333 333 The file is executed in a namespace initially consisting only of
334 334 __name__=='__main__' and sys.argv constructed as indicated. It thus
335 335 sees its environment as if it were being run as a stand-alone program
336 336 (except for sharing global objects such as previously imported
337 337 modules). But after execution, the IPython interactive namespace gets
338 338 updated with all variables defined in the program (except for __name__
339 339 and sys.argv). This allows for very convenient loading of code for
340 340 interactive work, while giving each program a 'clean sheet' to run in.
341 341
342 342 Options:
343 343
344 344 -n: __name__ is NOT set to '__main__', but to the running file's name
345 345 without extension (as python does under import). This allows running
346 346 scripts and reloading the definitions in them without calling code
347 347 protected by an ' if __name__ == "__main__" ' clause.
348 348
349 349 -i: run the file in IPython's namespace instead of an empty one. This
350 350 is useful if you are experimenting with code written in a text editor
351 351 which depends on variables defined interactively.
352 352
353 353 -e: ignore sys.exit() calls or SystemExit exceptions in the script
354 354 being run. This is particularly useful if IPython is being used to
355 355 run unittests, which always exit with a sys.exit() call. In such
356 356 cases you are interested in the output of the test results, not in
357 357 seeing a traceback of the unittest module.
358 358
359 359 -t: print timing information at the end of the run. IPython will give
360 360 you an estimated CPU time consumption for your script, which under
361 361 Unix uses the resource module to avoid the wraparound problems of
362 362 time.clock(). Under Unix, an estimate of time spent on system tasks
363 363 is also given (for Windows platforms this is reported as 0.0).
364 364
365 365 If -t is given, an additional -N<N> option can be given, where <N>
366 366 must be an integer indicating how many times you want the script to
367 367 run. The final timing report will include total and per run results.
368 368
369 369 For example (testing the script uniq_stable.py)::
370 370
371 371 In [1]: run -t uniq_stable
372 372
373 373 IPython CPU timings (estimated):\\
374 374 User : 0.19597 s.\\
375 375 System: 0.0 s.\\
376 376
377 377 In [2]: run -t -N5 uniq_stable
378 378
379 379 IPython CPU timings (estimated):\\
380 380 Total runs performed: 5\\
381 381 Times : Total Per run\\
382 382 User : 0.910862 s, 0.1821724 s.\\
383 383 System: 0.0 s, 0.0 s.
384 384
385 385 -d: run your program under the control of pdb, the Python debugger.
386 386 This allows you to execute your program step by step, watch variables,
387 387 etc. Internally, what IPython does is similar to calling:
388 388
389 389 pdb.run('execfile("YOURFILENAME")')
390 390
391 391 with a breakpoint set on line 1 of your file. You can change the line
392 392 number for this automatic breakpoint to be <N> by using the -bN option
393 393 (where N must be an integer). For example::
394 394
395 395 %run -d -b40 myscript
396 396
397 397 will set the first breakpoint at line 40 in myscript.py. Note that
398 398 the first breakpoint must be set on a line which actually does
399 399 something (not a comment or docstring) for it to stop execution.
400 400
401 401 When the pdb debugger starts, you will see a (Pdb) prompt. You must
402 402 first enter 'c' (without quotes) to start execution up to the first
403 403 breakpoint.
404 404
405 405 Entering 'help' gives information about the use of the debugger. You
406 406 can easily see pdb's full documentation with "import pdb;pdb.help()"
407 407 at a prompt.
408 408
409 409 -p: run program under the control of the Python profiler module (which
410 410 prints a detailed report of execution times, function calls, etc).
411 411
412 412 You can pass other options after -p which affect the behavior of the
413 413 profiler itself. See the docs for %prun for details.
414 414
415 415 In this mode, the program's variables do NOT propagate back to the
416 416 IPython interactive namespace (because they remain in the namespace
417 417 where the profiler executes them).
418 418
419 419 Internally this triggers a call to %prun, see its documentation for
420 420 details on the options available specifically for profiling.
421 421
422 422 There is one special usage for which the text above doesn't apply:
423 423 if the filename ends with .ipy, the file is run as ipython script,
424 424 just as if the commands were written on IPython prompt.
425 425
426 426 -m: specify module name to load instead of script path. Similar to
427 427 the -m option for the python interpreter. Use this option last if you
428 428 want to combine with other %run options. Unlike the python interpreter
429 429 only source modules are allowed no .pyc or .pyo files.
430 430 For example::
431 431
432 432 %run -m example
433 433
434 434 will run the example module.
435 435
436 436 """
437 437
438 438 # get arguments and set sys.argv for program to be run.
439 439 opts, arg_lst = self.parse_options(parameter_s, 'nidtN:b:pD:l:rs:T:em:',
440 440 mode='list', list_all=1)
441 441 if "m" in opts:
442 442 modulename = opts["m"][0]
443 443 modpath = find_mod(modulename)
444 444 if modpath is None:
445 445 warn('%r is not a valid modulename on sys.path'%modulename)
446 446 return
447 447 arg_lst = [modpath] + arg_lst
448 448 try:
449 449 filename = file_finder(arg_lst[0])
450 450 except IndexError:
451 451 warn('you must provide at least a filename.')
452 452 print '\n%run:\n', oinspect.getdoc(self.run)
453 453 return
454 454 except IOError as e:
455 455 try:
456 456 msg = str(e)
457 457 except UnicodeError:
458 458 msg = e.message
459 459 error(msg)
460 460 return
461 461
462 462 if filename.lower().endswith('.ipy'):
463 463 self.shell.safe_execfile_ipy(filename)
464 464 return
465 465
466 466 # Control the response to exit() calls made by the script being run
467 467 exit_ignore = 'e' in opts
468 468
469 469 # Make sure that the running script gets a proper sys.argv as if it
470 470 # were run from a system shell.
471 471 save_argv = sys.argv # save it for later restoring
472 472
473 473 # simulate shell expansion on arguments, at least tilde expansion
474 474 args = [ os.path.expanduser(a) for a in arg_lst[1:] ]
475 475
476 476 sys.argv = [filename] + args # put in the proper filename
477 477 # protect sys.argv from potential unicode strings on Python 2:
478 478 if not py3compat.PY3:
479 479 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
480 480
481 481 if 'i' in opts:
482 482 # Run in user's interactive namespace
483 483 prog_ns = self.shell.user_ns
484 484 __name__save = self.shell.user_ns['__name__']
485 485 prog_ns['__name__'] = '__main__'
486 486 main_mod = self.shell.new_main_mod(prog_ns)
487 487 else:
488 488 # Run in a fresh, empty namespace
489 489 if 'n' in opts:
490 490 name = os.path.splitext(os.path.basename(filename))[0]
491 491 else:
492 492 name = '__main__'
493 493
494 494 main_mod = self.shell.new_main_mod()
495 495 prog_ns = main_mod.__dict__
496 496 prog_ns['__name__'] = name
497 497
498 498 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
499 499 # set the __file__ global in the script's namespace
500 500 prog_ns['__file__'] = filename
501 501
502 502 # pickle fix. See interactiveshell for an explanation. But we need to
503 503 # make sure that, if we overwrite __main__, we replace it at the end
504 504 main_mod_name = prog_ns['__name__']
505 505
506 506 if main_mod_name == '__main__':
507 507 restore_main = sys.modules['__main__']
508 508 else:
509 509 restore_main = False
510 510
511 511 # This needs to be undone at the end to prevent holding references to
512 512 # every single object ever created.
513 513 sys.modules[main_mod_name] = main_mod
514 514
515 515 try:
516 516 stats = None
517 517 with self.shell.readline_no_record:
518 518 if 'p' in opts:
519 519 stats = self.prun('', 0, opts, arg_lst, prog_ns)
520 520 else:
521 521 if 'd' in opts:
522 522 deb = debugger.Pdb(self.shell.colors)
523 523 # reset Breakpoint state, which is moronically kept
524 524 # in a class
525 525 bdb.Breakpoint.next = 1
526 526 bdb.Breakpoint.bplist = {}
527 527 bdb.Breakpoint.bpbynumber = [None]
528 528 # Set an initial breakpoint to stop execution
529 529 maxtries = 10
530 530 bp = int(opts.get('b', [1])[0])
531 531 checkline = deb.checkline(filename, bp)
532 532 if not checkline:
533 533 for bp in range(bp + 1, bp + maxtries + 1):
534 534 if deb.checkline(filename, bp):
535 535 break
536 536 else:
537 537 msg = ("\nI failed to find a valid line to set "
538 538 "a breakpoint\n"
539 539 "after trying up to line: %s.\n"
540 540 "Please set a valid breakpoint manually "
541 541 "with the -b option." % bp)
542 542 error(msg)
543 543 return
544 544 # if we find a good linenumber, set the breakpoint
545 545 deb.do_break('%s:%s' % (filename, bp))
546 546 # Start file run
547 547 print "NOTE: Enter 'c' at the",
548 548 print "%s prompt to start your script." % deb.prompt
549 549 ns = {'execfile': py3compat.execfile, 'prog_ns': prog_ns}
550 550 try:
551 551 deb.run('execfile("%s", prog_ns)' % filename, ns)
552 552
553 553 except:
554 554 etype, value, tb = sys.exc_info()
555 555 # Skip three frames in the traceback: the %run one,
556 556 # one inside bdb.py, and the command-line typed by the
557 557 # user (run by exec in pdb itself).
558 558 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
559 559 else:
560 560 if runner is None:
561 561 runner = self.default_runner
562 562 if runner is None:
563 563 runner = self.shell.safe_execfile
564 564 if 't' in opts:
565 565 # timed execution
566 566 try:
567 567 nruns = int(opts['N'][0])
568 568 if nruns < 1:
569 569 error('Number of runs must be >=1')
570 570 return
571 571 except (KeyError):
572 572 nruns = 1
573 573 twall0 = time.time()
574 574 if nruns == 1:
575 575 t0 = clock2()
576 576 runner(filename, prog_ns, prog_ns,
577 577 exit_ignore=exit_ignore)
578 578 t1 = clock2()
579 579 t_usr = t1[0] - t0[0]
580 580 t_sys = t1[1] - t0[1]
581 581 print "\nIPython CPU timings (estimated):"
582 582 print " User : %10.2f s." % t_usr
583 583 print " System : %10.2f s." % t_sys
584 584 else:
585 585 runs = range(nruns)
586 586 t0 = clock2()
587 587 for nr in runs:
588 588 runner(filename, prog_ns, prog_ns,
589 589 exit_ignore=exit_ignore)
590 590 t1 = clock2()
591 591 t_usr = t1[0] - t0[0]
592 592 t_sys = t1[1] - t0[1]
593 593 print "\nIPython CPU timings (estimated):"
594 594 print "Total runs performed:", nruns
595 595 print " Times : %10.2f %10.2f" % ('Total', 'Per run')
596 596 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
597 597 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
598 598 twall1 = time.time()
599 599 print "Wall time: %10.2f s." % (twall1 - twall0)
600 600
601 601 else:
602 602 # regular execution
603 603 runner(filename, prog_ns, prog_ns, exit_ignore=exit_ignore)
604 604
605 605 if 'i' in opts:
606 606 self.shell.user_ns['__name__'] = __name__save
607 607 else:
608 608 # The shell MUST hold a reference to prog_ns so after %run
609 609 # exits, the python deletion mechanism doesn't zero it out
610 610 # (leaving dangling references).
611 611 self.shell.cache_main_mod(prog_ns, filename)
612 612 # update IPython interactive namespace
613 613
614 614 # Some forms of read errors on the file may mean the
615 615 # __name__ key was never set; using pop we don't have to
616 616 # worry about a possible KeyError.
617 617 prog_ns.pop('__name__', None)
618 618
619 619 self.shell.user_ns.update(prog_ns)
620 620 finally:
621 621 # It's a bit of a mystery why, but __builtins__ can change from
622 622 # being a module to becoming a dict missing some key data after
623 623 # %run. As best I can see, this is NOT something IPython is doing
624 624 # at all, and similar problems have been reported before:
625 625 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
626 626 # Since this seems to be done by the interpreter itself, the best
627 627 # we can do is to at least restore __builtins__ for the user on
628 628 # exit.
629 629 self.shell.user_ns['__builtins__'] = builtin_mod
630 630
631 631 # Ensure key global structures are restored
632 632 sys.argv = save_argv
633 633 if restore_main:
634 634 sys.modules['__main__'] = restore_main
635 635 else:
636 636 # Remove from sys.modules the reference to main_mod we'd
637 637 # added. Otherwise it will trap references to objects
638 638 # contained therein.
639 639 del sys.modules[main_mod_name]
640 640
641 641 return stats
642 642
643 643 @skip_doctest
644 644 @line_magic
645 645 def timeit(self, parameter_s=''):
646 646 """Time execution of a Python statement or expression
647 647
648 648 Usage:\\
649 649 %timeit [-n<N> -r<R> [-t|-c]] statement
650 650
651 651 Time execution of a Python statement or expression using the timeit
652 652 module.
653 653
654 654 Options:
655 655 -n<N>: execute the given statement <N> times in a loop. If this value
656 656 is not given, a fitting value is chosen.
657 657
658 658 -r<R>: repeat the loop iteration <R> times and take the best result.
659 659 Default: 3
660 660
661 661 -t: use time.time to measure the time, which is the default on Unix.
662 662 This function measures wall time.
663 663
664 664 -c: use time.clock to measure the time, which is the default on
665 665 Windows and measures wall time. On Unix, resource.getrusage is used
666 666 instead and returns the CPU user time.
667 667
668 668 -p<P>: use a precision of <P> digits to display the timing result.
669 669 Default: 3
670 670
671 671
672 672 Examples
673 673 --------
674 674 ::
675 675
676 676 In [1]: %timeit pass
677 677 10000000 loops, best of 3: 53.3 ns per loop
678 678
679 679 In [2]: u = None
680 680
681 681 In [3]: %timeit u is None
682 682 10000000 loops, best of 3: 184 ns per loop
683 683
684 684 In [4]: %timeit -r 4 u == None
685 685 1000000 loops, best of 4: 242 ns per loop
686 686
687 687 In [5]: import time
688 688
689 689 In [6]: %timeit -n1 time.sleep(2)
690 690 1 loops, best of 3: 2 s per loop
691 691
692 692
693 693 The times reported by %timeit will be slightly higher than those
694 694 reported by the timeit.py script when variables are accessed. This is
695 695 due to the fact that %timeit executes the statement in the namespace
696 696 of the shell, compared with timeit.py, which uses a single setup
697 697 statement to import function or create variables. Generally, the bias
698 698 does not matter as long as results from timeit.py are not mixed with
699 699 those from %timeit."""
700 700
701 701 import timeit
702 702 import math
703 703
704 704 # XXX: Unfortunately the unicode 'micro' symbol can cause problems in
705 705 # certain terminals. Until we figure out a robust way of
706 706 # auto-detecting if the terminal can deal with it, use plain 'us' for
707 707 # microseconds. I am really NOT happy about disabling the proper
708 708 # 'micro' prefix, but crashing is worse... If anyone knows what the
709 709 # right solution for this is, I'm all ears...
710 710 #
711 711 # Note: using
712 712 #
713 713 # s = u'\xb5'
714 714 # s.encode(sys.getdefaultencoding())
715 715 #
716 716 # is not sufficient, as I've seen terminals where that fails but
717 717 # print s
718 718 #
719 719 # succeeds
720 720 #
721 721 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
722 722
723 723 #units = [u"s", u"ms",u'\xb5',"ns"]
724 724 units = [u"s", u"ms",u'us',"ns"]
725 725
726 726 scaling = [1, 1e3, 1e6, 1e9]
727 727
728 728 opts, stmt = self.parse_options(parameter_s,'n:r:tcp:',
729 729 posix=False, strict=False)
730 730 if stmt == "":
731 731 return
732 732 timefunc = timeit.default_timer
733 733 number = int(getattr(opts, "n", 0))
734 734 repeat = int(getattr(opts, "r", timeit.default_repeat))
735 735 precision = int(getattr(opts, "p", 3))
736 736 if hasattr(opts, "t"):
737 737 timefunc = time.time
738 738 if hasattr(opts, "c"):
739 739 timefunc = clock
740 740
741 741 timer = timeit.Timer(timer=timefunc)
742 742 # this code has tight coupling to the inner workings of timeit.Timer,
743 743 # but is there a better way to achieve that the code stmt has access
744 744 # to the shell namespace?
745 745
746 746 src = timeit.template % {'stmt': timeit.reindent(stmt, 8),
747 747 'setup': "pass"}
748 748 # Track compilation time so it can be reported if too long
749 749 # Minimum time above which compilation time will be reported
750 750 tc_min = 0.1
751 751
752 752 t0 = clock()
753 753 code = compile(src, "<magic-timeit>", "exec")
754 754 tc = clock()-t0
755 755
756 756 ns = {}
757 757 exec code in self.shell.user_ns, ns
758 758 timer.inner = ns["inner"]
759 759
760 760 if number == 0:
761 761 # determine number so that 0.2 <= total time < 2.0
762 762 number = 1
763 763 for i in range(1, 10):
764 764 if timer.timeit(number) >= 0.2:
765 765 break
766 766 number *= 10
767 767
768 768 best = min(timer.repeat(repeat, number)) / number
769 769
770 770 if best > 0.0 and best < 1000.0:
771 771 order = min(-int(math.floor(math.log10(best)) // 3), 3)
772 772 elif best >= 1000.0:
773 773 order = 0
774 774 else:
775 775 order = 3
776 776 print u"%d loops, best of %d: %.*g %s per loop" % (number, repeat,
777 777 precision,
778 778 best * scaling[order],
779 779 units[order])
780 780 if tc > tc_min:
781 781 print "Compiler time: %.2f s" % tc
782 782
783 783 @skip_doctest
784 784 @needs_local_scope
785 785 @line_magic
786 786 def time(self,parameter_s, user_locals):
787 787 """Time execution of a Python statement or expression.
788 788
789 789 The CPU and wall clock times are printed, and the value of the
790 790 expression (if any) is returned. Note that under Win32, system time
791 791 is always reported as 0, since it can not be measured.
792 792
793 793 This function provides very basic timing functionality. In Python
794 794 2.3, the timeit module offers more control and sophistication, so this
795 795 could be rewritten to use it (patches welcome).
796 796
797 797 Examples
798 798 --------
799 799 ::
800 800
801 801 In [1]: time 2**128
802 802 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
803 803 Wall time: 0.00
804 804 Out[1]: 340282366920938463463374607431768211456L
805 805
806 806 In [2]: n = 1000000
807 807
808 808 In [3]: time sum(range(n))
809 809 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
810 810 Wall time: 1.37
811 811 Out[3]: 499999500000L
812 812
813 813 In [4]: time print 'hello world'
814 814 hello world
815 815 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
816 816 Wall time: 0.00
817 817
818 818 Note that the time needed by Python to compile the given expression
819 819 will be reported if it is more than 0.1s. In this example, the
820 820 actual exponentiation is done by Python at compilation time, so while
821 821 the expression can take a noticeable amount of time to compute, that
822 822 time is purely due to the compilation:
823 823
824 824 In [5]: time 3**9999;
825 825 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
826 826 Wall time: 0.00 s
827 827
828 828 In [6]: time 3**999999;
829 829 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
830 830 Wall time: 0.00 s
831 831 Compiler : 0.78 s
832 832 """
833 833
834 834 # fail immediately if the given expression can't be compiled
835 835
836 836 expr = self.shell.prefilter(parameter_s,False)
837 837
838 838 # Minimum time above which compilation time will be reported
839 839 tc_min = 0.1
840 840
841 841 try:
842 842 mode = 'eval'
843 843 t0 = clock()
844 844 code = compile(expr,'<timed eval>',mode)
845 845 tc = clock()-t0
846 846 except SyntaxError:
847 847 mode = 'exec'
848 848 t0 = clock()
849 849 code = compile(expr,'<timed exec>',mode)
850 850 tc = clock()-t0
851 851 # skew measurement as little as possible
852 852 glob = self.shell.user_ns
853 853 wtime = time.time
854 854 # time execution
855 855 wall_st = wtime()
856 856 if mode=='eval':
857 857 st = clock2()
858 858 out = eval(code, glob, user_locals)
859 859 end = clock2()
860 860 else:
861 861 st = clock2()
862 862 exec code in glob, user_locals
863 863 end = clock2()
864 864 out = None
865 865 wall_end = wtime()
866 866 # Compute actual times and report
867 867 wall_time = wall_end-wall_st
868 868 cpu_user = end[0]-st[0]
869 869 cpu_sys = end[1]-st[1]
870 870 cpu_tot = cpu_user+cpu_sys
871 871 print "CPU times: user %.2f s, sys: %.2f s, total: %.2f s" % \
872 872 (cpu_user,cpu_sys,cpu_tot)
873 873 print "Wall time: %.2f s" % wall_time
874 874 if tc > tc_min:
875 875 print "Compiler : %.2f s" % tc
876 876 return out
877 877
878 878 @skip_doctest
879 879 @line_magic
880 880 def macro(self, parameter_s=''):
881 881 """Define a macro for future re-execution. It accepts ranges of history,
882 882 filenames or string objects.
883 883
884 884 Usage:\\
885 885 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
886 886
887 887 Options:
888 888
889 889 -r: use 'raw' input. By default, the 'processed' history is used,
890 890 so that magics are loaded in their transformed version to valid
891 891 Python. If this option is given, the raw input as typed as the
892 892 command line is used instead.
893 893
894 894 This will define a global variable called `name` which is a string
895 895 made of joining the slices and lines you specify (n1,n2,... numbers
896 896 above) from your input history into a single string. This variable
897 897 acts like an automatic function which re-executes those lines as if
898 898 you had typed them. You just type 'name' at the prompt and the code
899 899 executes.
900 900
901 901 The syntax for indicating input ranges is described in %history.
902 902
903 903 Note: as a 'hidden' feature, you can also use traditional python slice
904 904 notation, where N:M means numbers N through M-1.
905 905
906 906 For example, if your history contains (%hist prints it)::
907 907
908 908 44: x=1
909 909 45: y=3
910 910 46: z=x+y
911 911 47: print x
912 912 48: a=5
913 913 49: print 'x',x,'y',y
914 914
915 915 you can create a macro with lines 44 through 47 (included) and line 49
916 916 called my_macro with::
917 917
918 918 In [55]: %macro my_macro 44-47 49
919 919
920 920 Now, typing `my_macro` (without quotes) will re-execute all this code
921 921 in one pass.
922 922
923 923 You don't need to give the line-numbers in order, and any given line
924 924 number can appear multiple times. You can assemble macros with any
925 925 lines from your input history in any order.
926 926
927 927 The macro is a simple object which holds its value in an attribute,
928 928 but IPython's display system checks for macros and executes them as
929 929 code instead of printing them when you type their name.
930 930
931 931 You can view a macro's contents by explicitly printing it with::
932 932
933 933 print macro_name
934 934
935 935 """
936 936 opts,args = self.parse_options(parameter_s,'r',mode='list')
937 937 if not args: # List existing macros
938 938 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
939 939 isinstance(v, Macro))
940 940 if len(args) == 1:
941 941 raise UsageError(
942 942 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
943 943 name, codefrom = args[0], " ".join(args[1:])
944 944
945 945 #print 'rng',ranges # dbg
946 946 try:
947 947 lines = self.shell.find_user_code(codefrom, 'r' in opts)
948 948 except (ValueError, TypeError) as e:
949 949 print e.args[0]
950 950 return
951 951 macro = Macro(lines)
952 952 self.shell.define_macro(name, macro)
953 953 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
954 954 print '=== Macro contents: ==='
955 955 print macro,
@@ -1,69 +1,69 b''
1 1 """Implementation of magic functions for the extension machinery.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 # Stdlib
16 16 import os
17 17
18 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 22 # Magic implementation classes
23 23 #-----------------------------------------------------------------------------
24 24
25 @register_magics
25 @magics_class
26 26 class ExtensionMagics(Magics):
27 27 """Magics to manage the IPython extensions system."""
28 28
29 29 @line_magic
30 30 def install_ext(self, parameter_s=''):
31 31 """Download and install an extension from a URL, e.g.::
32 32
33 33 %install_ext https://bitbucket.org/birkenfeld/ipython-physics/raw/d1310a2ab15d/physics.py
34 34
35 35 The URL should point to an importable Python module - either a .py file
36 36 or a .zip file.
37 37
38 38 Parameters:
39 39
40 40 -n filename : Specify a name for the file, rather than taking it from
41 41 the URL.
42 42 """
43 43 opts, args = self.parse_options(parameter_s, 'n:')
44 44 try:
45 45 filename = self.shell.extension_manager.install_extension(args,
46 46 opts.get('n'))
47 47 except ValueError as e:
48 48 print e
49 49 return
50 50
51 51 filename = os.path.basename(filename)
52 52 print "Installed %s. To use it, type:" % filename
53 53 print " %%load_ext %s" % os.path.splitext(filename)[0]
54 54
55 55
56 56 @line_magic
57 57 def load_ext(self, module_str):
58 58 """Load an IPython extension by its module name."""
59 59 return self.shell.extension_manager.load_extension(module_str)
60 60
61 61 @line_magic
62 62 def unload_ext(self, module_str):
63 63 """Unload an IPython extension by its module name."""
64 64 self.shell.extension_manager.unload_extension(module_str)
65 65
66 66 @line_magic
67 67 def reload_ext(self, module_str):
68 68 """Reload an IPython extension by its module name."""
69 69 self.shell.extension_manager.reload_extension(module_str)
@@ -1,294 +1,294 b''
1 1 """Implementation of magic functions related to History.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012, IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14 from __future__ import print_function
15 15
16 16 # Stdlib
17 17 import os
18 18 from io import open as io_open
19 19
20 20 # Our own packages
21 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 23 from IPython.testing.skipdoctest import skip_doctest
24 24 from IPython.utils import io
25 25
26 26 #-----------------------------------------------------------------------------
27 27 # Magics class implementation
28 28 #-----------------------------------------------------------------------------
29 29
30 @register_magics
30 @magics_class
31 31 class HistoryMagics(Magics):
32 32
33 33 @skip_doctest
34 34 @line_magic
35 35 def history(self, parameter_s = ''):
36 36 """Print input history (_i<n> variables), with most recent last.
37 37
38 38 %history [-o -p -t -n] [-f filename] [range | -g pattern | -l number]
39 39
40 40 By default, input history is printed without line numbers so it can be
41 41 directly pasted into an editor. Use -n to show them.
42 42
43 43 By default, all input history from the current session is displayed.
44 44 Ranges of history can be indicated using the syntax:
45 45 4 : Line 4, current session
46 46 4-6 : Lines 4-6, current session
47 47 243/1-5: Lines 1-5, session 243
48 48 ~2/7 : Line 7, session 2 before current
49 49 ~8/1-~6/5 : From the first line of 8 sessions ago, to the fifth line
50 50 of 6 sessions ago.
51 51 Multiple ranges can be entered, separated by spaces
52 52
53 53 The same syntax is used by %macro, %save, %edit, %rerun
54 54
55 55 Options:
56 56
57 57 -n: print line numbers for each input.
58 58 This feature is only available if numbered prompts are in use.
59 59
60 60 -o: also print outputs for each input.
61 61
62 62 -p: print classic '>>>' python prompts before each input. This is
63 63 useful for making documentation, and in conjunction with -o, for
64 64 producing doctest-ready output.
65 65
66 66 -r: (default) print the 'raw' history, i.e. the actual commands you
67 67 typed.
68 68
69 69 -t: print the 'translated' history, as IPython understands it.
70 70 IPython filters your input and converts it all into valid Python
71 71 source before executing it (things like magics or aliases are turned
72 72 into function calls, for example). With this option, you'll see the
73 73 native history instead of the user-entered version: '%cd /' will be
74 74 seen as 'get_ipython().magic("%cd /")' instead of '%cd /'.
75 75
76 76 -g: treat the arg as a pattern to grep for in (full) history.
77 77 This includes the saved history (almost all commands ever written).
78 78 Use '%hist -g' to show full saved history (may be very long).
79 79
80 80 -l: get the last n lines from all sessions. Specify n as a single
81 81 arg, or the default is the last 10 lines.
82 82
83 83 -f FILENAME: instead of printing the output to the screen, redirect
84 84 it to the given file. The file is always overwritten, though *when
85 85 it can*, IPython asks for confirmation first. In particular, running
86 86 the command 'history -f FILENAME' from the IPython Notebook
87 87 interface will replace FILENAME even if it already exists *without*
88 88 confirmation.
89 89
90 90 Examples
91 91 --------
92 92 ::
93 93
94 94 In [6]: %hist -n 4-6
95 95 4:a = 12
96 96 5:print a**2
97 97 6:%hist -n 4-6
98 98
99 99 """
100 100
101 101 if not self.shell.displayhook.do_full_cache:
102 102 print('This feature is only available if numbered prompts '
103 103 'are in use.')
104 104 return
105 105 opts,args = self.parse_options(parameter_s,'noprtglf:',mode='string')
106 106
107 107 # For brevity
108 108 history_manager = self.shell.history_manager
109 109
110 110 def _format_lineno(session, line):
111 111 """Helper function to format line numbers properly."""
112 112 if session in (0, history_manager.session_number):
113 113 return str(line)
114 114 return "%s/%s" % (session, line)
115 115
116 116 # Check if output to specific file was requested.
117 117 try:
118 118 outfname = opts['f']
119 119 except KeyError:
120 120 outfile = io.stdout # default
121 121 # We don't want to close stdout at the end!
122 122 close_at_end = False
123 123 else:
124 124 if os.path.exists(outfname):
125 125 try:
126 126 ans = io.ask_yes_no("File %r exists. Overwrite?" % outfname)
127 127 except StdinNotImplementedError:
128 128 ans = True
129 129 if not ans:
130 130 print('Aborting.')
131 131 return
132 132 print("Overwriting file.")
133 133 outfile = io_open(outfname, 'w', encoding='utf-8')
134 134 close_at_end = True
135 135
136 136 print_nums = 'n' in opts
137 137 get_output = 'o' in opts
138 138 pyprompts = 'p' in opts
139 139 # Raw history is the default
140 140 raw = not('t' in opts)
141 141
142 142 pattern = None
143 143
144 144 if 'g' in opts: # Glob search
145 145 pattern = "*" + args + "*" if args else "*"
146 146 hist = history_manager.search(pattern, raw=raw, output=get_output)
147 147 print_nums = True
148 148 elif 'l' in opts: # Get 'tail'
149 149 try:
150 150 n = int(args)
151 151 except (ValueError, IndexError):
152 152 n = 10
153 153 hist = history_manager.get_tail(n, raw=raw, output=get_output)
154 154 else:
155 155 if args: # Get history by ranges
156 156 hist = history_manager.get_range_by_str(args, raw, get_output)
157 157 else: # Just get history for the current session
158 158 hist = history_manager.get_range(raw=raw, output=get_output)
159 159
160 160 # We could be displaying the entire history, so let's not try to pull
161 161 # it into a list in memory. Anything that needs more space will just
162 162 # misalign.
163 163 width = 4
164 164
165 165 for session, lineno, inline in hist:
166 166 # Print user history with tabs expanded to 4 spaces. The GUI
167 167 # clients use hard tabs for easier usability in auto-indented code,
168 168 # but we want to produce PEP-8 compliant history for safe pasting
169 169 # into an editor.
170 170 if get_output:
171 171 inline, output = inline
172 172 inline = inline.expandtabs(4).rstrip()
173 173
174 174 multiline = "\n" in inline
175 175 line_sep = '\n' if multiline else ' '
176 176 if print_nums:
177 177 print(u'%s:%s' % (_format_lineno(session, lineno).rjust(width),
178 178 line_sep), file=outfile, end=u'')
179 179 if pyprompts:
180 180 print(u">>> ", end=u"", file=outfile)
181 181 if multiline:
182 182 inline = "\n... ".join(inline.splitlines()) + "\n..."
183 183 print(inline, file=outfile)
184 184 if get_output and output:
185 185 print(output, file=outfile)
186 186
187 187 if close_at_end:
188 188 outfile.close()
189 189
190 190 # For a long time we've had %hist as well as %history
191 191 @line_magic
192 192 def hist(self, arg):
193 193 return self.history(arg)
194 194
195 195 hist.__doc__ = history.__doc__
196 196
197 197 @line_magic
198 198 def rep(self, arg):
199 199 r"""Repeat a command, or get command to input line for editing.
200 200
201 201 %recall and %rep are equivalent.
202 202
203 203 - %recall (no arguments):
204 204
205 205 Place a string version of last computation result (stored in the
206 206 special '_' variable) to the next input prompt. Allows you to create
207 207 elaborate command lines without using copy-paste::
208 208
209 209 In[1]: l = ["hei", "vaan"]
210 210 In[2]: "".join(l)
211 211 Out[2]: heivaan
212 212 In[3]: %rep
213 213 In[4]: heivaan_ <== cursor blinking
214 214
215 215 %recall 45
216 216
217 217 Place history line 45 on the next input prompt. Use %hist to find
218 218 out the number.
219 219
220 220 %recall 1-4
221 221
222 222 Combine the specified lines into one cell, and place it on the next
223 223 input prompt. See %history for the slice syntax.
224 224
225 225 %recall foo+bar
226 226
227 227 If foo+bar can be evaluated in the user namespace, the result is
228 228 placed at the next input prompt. Otherwise, the history is searched
229 229 for lines which contain that substring, and the most recent one is
230 230 placed at the next input prompt.
231 231 """
232 232 if not arg: # Last output
233 233 self.shell.set_next_input(str(self.shell.user_ns["_"]))
234 234 return
235 235 # Get history range
236 236 histlines = self.shell.history_manager.get_range_by_str(arg)
237 237 cmd = "\n".join(x[2] for x in histlines)
238 238 if cmd:
239 239 self.shell.set_next_input(cmd.rstrip())
240 240 return
241 241
242 242 try: # Variable in user namespace
243 243 cmd = str(eval(arg, self.shell.user_ns))
244 244 except Exception: # Search for term in history
245 245 histlines = self.shell.history_manager.search("*"+arg+"*")
246 246 for h in reversed([x[2] for x in histlines]):
247 247 if 'rep' in h:
248 248 continue
249 249 self.shell.set_next_input(h.rstrip())
250 250 return
251 251 else:
252 252 self.shell.set_next_input(cmd.rstrip())
253 253 print("Couldn't evaluate or find in history:", arg)
254 254
255 255 @line_magic
256 256 def rerun(self, parameter_s=''):
257 257 """Re-run previous input
258 258
259 259 By default, you can specify ranges of input history to be repeated
260 260 (as with %history). With no arguments, it will repeat the last line.
261 261
262 262 Options:
263 263
264 264 -l <n> : Repeat the last n lines of input, not including the
265 265 current command.
266 266
267 267 -g foo : Repeat the most recent line which contains foo
268 268 """
269 269 opts, args = self.parse_options(parameter_s, 'l:g:', mode='string')
270 270 if "l" in opts: # Last n lines
271 271 n = int(opts['l'])
272 272 hist = self.shell.history_manager.get_tail(n)
273 273 elif "g" in opts: # Search
274 274 p = "*"+opts['g']+"*"
275 275 hist = list(self.shell.history_manager.search(p))
276 276 for l in reversed(hist):
277 277 if "rerun" not in l[2]:
278 278 hist = [l] # The last match which isn't a %rerun
279 279 break
280 280 else:
281 281 hist = [] # No matches except %rerun
282 282 elif args: # Specify history ranges
283 283 hist = self.shell.history_manager.get_range_by_str(args)
284 284 else: # Last line
285 285 hist = self.shell.history_manager.get_tail(1)
286 286 hist = [x[2] for x in hist]
287 287 if not hist:
288 288 print("No lines in history match specification")
289 289 return
290 290 histlines = "\n".join(hist)
291 291 print("=== Executing: ===")
292 292 print(histlines)
293 293 print("=== Output: ===")
294 294 self.shell.run_cell("\n".join(hist), store_history=False)
@@ -1,169 +1,169 b''
1 1 """Implementation of magic functions for IPython's own logging.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 # Stdlib
16 16 import os
17 17 import sys
18 18
19 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 21 from IPython.utils.warn import warn
22 22
23 23 #-----------------------------------------------------------------------------
24 24 # Magic implementation classes
25 25 #-----------------------------------------------------------------------------
26 26
27 @register_magics
27 @magics_class
28 28 class LoggingMagics(Magics):
29 29 """Magics related to all logging machinery."""
30 30
31 31 @line_magic
32 32 def logstart(self, parameter_s=''):
33 33 """Start logging anywhere in a session.
34 34
35 35 %logstart [-o|-r|-t] [log_name [log_mode]]
36 36
37 37 If no name is given, it defaults to a file named 'ipython_log.py' in your
38 38 current directory, in 'rotate' mode (see below).
39 39
40 40 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
41 41 history up to that point and then continues logging.
42 42
43 43 %logstart takes a second optional parameter: logging mode. This can be one
44 44 of (note that the modes are given unquoted):\\
45 45 append: well, that says it.\\
46 46 backup: rename (if exists) to name~ and start name.\\
47 47 global: single logfile in your home dir, appended to.\\
48 48 over : overwrite existing log.\\
49 49 rotate: create rotating logs name.1~, name.2~, etc.
50 50
51 51 Options:
52 52
53 53 -o: log also IPython's output. In this mode, all commands which
54 54 generate an Out[NN] prompt are recorded to the logfile, right after
55 55 their corresponding input line. The output lines are always
56 56 prepended with a '#[Out]# ' marker, so that the log remains valid
57 57 Python code.
58 58
59 59 Since this marker is always the same, filtering only the output from
60 60 a log is very easy, using for example a simple awk call::
61 61
62 62 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
63 63
64 64 -r: log 'raw' input. Normally, IPython's logs contain the processed
65 65 input, so that user lines are logged in their final form, converted
66 66 into valid Python. For example, %Exit is logged as
67 67 _ip.magic("Exit"). If the -r flag is given, all input is logged
68 68 exactly as typed, with no transformations applied.
69 69
70 70 -t: put timestamps before each input line logged (these are put in
71 71 comments)."""
72 72
73 73 opts,par = self.parse_options(parameter_s,'ort')
74 74 log_output = 'o' in opts
75 75 log_raw_input = 'r' in opts
76 76 timestamp = 't' in opts
77 77
78 78 logger = self.shell.logger
79 79
80 80 # if no args are given, the defaults set in the logger constructor by
81 81 # ipython remain valid
82 82 if par:
83 83 try:
84 84 logfname,logmode = par.split()
85 85 except:
86 86 logfname = par
87 87 logmode = 'backup'
88 88 else:
89 89 logfname = logger.logfname
90 90 logmode = logger.logmode
91 91 # put logfname into rc struct as if it had been called on the command
92 92 # line, so it ends up saved in the log header Save it in case we need
93 93 # to restore it...
94 94 old_logfile = self.shell.logfile
95 95 if logfname:
96 96 logfname = os.path.expanduser(logfname)
97 97 self.shell.logfile = logfname
98 98
99 99 loghead = '# IPython log file\n\n'
100 100 try:
101 101 logger.logstart(logfname, loghead, logmode, log_output, timestamp,
102 102 log_raw_input)
103 103 except:
104 104 self.shell.logfile = old_logfile
105 105 warn("Couldn't start log: %s" % sys.exc_info()[1])
106 106 else:
107 107 # log input history up to this point, optionally interleaving
108 108 # output if requested
109 109
110 110 if timestamp:
111 111 # disable timestamping for the previous history, since we've
112 112 # lost those already (no time machine here).
113 113 logger.timestamp = False
114 114
115 115 if log_raw_input:
116 116 input_hist = self.shell.history_manager.input_hist_raw
117 117 else:
118 118 input_hist = self.shell.history_manager.input_hist_parsed
119 119
120 120 if log_output:
121 121 log_write = logger.log_write
122 122 output_hist = self.shell.history_manager.output_hist
123 123 for n in range(1,len(input_hist)-1):
124 124 log_write(input_hist[n].rstrip() + '\n')
125 125 if n in output_hist:
126 126 log_write(repr(output_hist[n]),'output')
127 127 else:
128 128 logger.log_write('\n'.join(input_hist[1:]))
129 129 logger.log_write('\n')
130 130 if timestamp:
131 131 # re-enable timestamping
132 132 logger.timestamp = True
133 133
134 134 print ('Activating auto-logging. '
135 135 'Current session state plus future input saved.')
136 136 logger.logstate()
137 137
138 138 @line_magic
139 139 def logstop(self, parameter_s=''):
140 140 """Fully stop logging and close log file.
141 141
142 142 In order to start logging again, a new %logstart call needs to be made,
143 143 possibly (though not necessarily) with a new filename, mode and other
144 144 options."""
145 145 self.logger.logstop()
146 146
147 147 @line_magic
148 148 def logoff(self, parameter_s=''):
149 149 """Temporarily stop logging.
150 150
151 151 You must have previously started logging."""
152 152 self.shell.logger.switch_log(0)
153 153
154 154 @line_magic
155 155 def logon(self, parameter_s=''):
156 156 """Restart logging.
157 157
158 158 This function is for restarting logging which you've temporarily
159 159 stopped with %logoff. For starting logging for the first time, you
160 160 must use the %logstart function, which allows you to specify an
161 161 optional log filename."""
162 162
163 163 self.shell.logger.switch_log(1)
164 164
165 165 @line_magic
166 166 def logstate(self, parameter_s=''):
167 167 """Print the status of the logging system."""
168 168
169 169 self.shell.logger.logstate()
@@ -1,702 +1,702 b''
1 1 """Implementation of namespace-related magic functions.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 # Stdlib
16 16 import gc
17 17 import re
18 18 import sys
19 19
20 20 # Our own packages
21 21 from IPython.core import page
22 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 24 from IPython.testing.skipdoctest import skip_doctest
25 25 from IPython.utils.encoding import DEFAULT_ENCODING
26 26 from IPython.utils.path import get_py_filename
27 27
28 28 #-----------------------------------------------------------------------------
29 29 # Magic implementation classes
30 30 #-----------------------------------------------------------------------------
31 31
32 @register_magics
32 @magics_class
33 33 class NamespaceMagics(Magics):
34 34 """Magics to manage various aspects of the user's namespace.
35 35
36 36 These include listing variables, introspecting into them, etc.
37 37 """
38 38
39 39 @line_magic
40 40 def pinfo(self, parameter_s='', namespaces=None):
41 41 """Provide detailed information about an object.
42 42
43 43 '%pinfo object' is just a synonym for object? or ?object."""
44 44
45 45 #print 'pinfo par: <%s>' % parameter_s # dbg
46 46
47 47
48 48 # detail_level: 0 -> obj? , 1 -> obj??
49 49 detail_level = 0
50 50 # We need to detect if we got called as 'pinfo pinfo foo', which can
51 51 # happen if the user types 'pinfo foo?' at the cmd line.
52 52 pinfo,qmark1,oname,qmark2 = \
53 53 re.match('(pinfo )?(\?*)(.*?)(\??$)',parameter_s).groups()
54 54 if pinfo or qmark1 or qmark2:
55 55 detail_level = 1
56 56 if "*" in oname:
57 57 self.psearch(oname)
58 58 else:
59 59 self.shell._inspect('pinfo', oname, detail_level=detail_level,
60 60 namespaces=namespaces)
61 61
62 62 @line_magic
63 63 def pinfo2(self, parameter_s='', namespaces=None):
64 64 """Provide extra detailed information about an object.
65 65
66 66 '%pinfo2 object' is just a synonym for object?? or ??object."""
67 67 self.shell._inspect('pinfo', parameter_s, detail_level=1,
68 68 namespaces=namespaces)
69 69
70 70 @skip_doctest
71 71 @line_magic
72 72 def pdef(self, parameter_s='', namespaces=None):
73 73 """Print the definition header for any callable object.
74 74
75 75 If the object is a class, print the constructor information.
76 76
77 77 Examples
78 78 --------
79 79 ::
80 80
81 81 In [3]: %pdef urllib.urlopen
82 82 urllib.urlopen(url, data=None, proxies=None)
83 83 """
84 84 self._inspect('pdef',parameter_s, namespaces)
85 85
86 86 @line_magic
87 87 def pdoc(self, parameter_s='', namespaces=None):
88 88 """Print the docstring for an object.
89 89
90 90 If the given object is a class, it will print both the class and the
91 91 constructor docstrings."""
92 92 self._inspect('pdoc',parameter_s, namespaces)
93 93
94 94 @line_magic
95 95 def psource(self, parameter_s='', namespaces=None):
96 96 """Print (or run through pager) the source code for an object."""
97 97 self._inspect('psource',parameter_s, namespaces)
98 98
99 99 @line_magic
100 100 def pfile(self, parameter_s=''):
101 101 """Print (or run through pager) the file where an object is defined.
102 102
103 103 The file opens at the line where the object definition begins. IPython
104 104 will honor the environment variable PAGER if set, and otherwise will
105 105 do its best to print the file in a convenient form.
106 106
107 107 If the given argument is not an object currently defined, IPython will
108 108 try to interpret it as a filename (automatically adding a .py extension
109 109 if needed). You can thus use %pfile as a syntax highlighting code
110 110 viewer."""
111 111
112 112 # first interpret argument as an object name
113 113 out = self._inspect('pfile',parameter_s)
114 114 # if not, try the input as a filename
115 115 if out == 'not found':
116 116 try:
117 117 filename = get_py_filename(parameter_s)
118 118 except IOError,msg:
119 119 print msg
120 120 return
121 121 page.page(self.shell.inspector.format(open(filename).read()))
122 122
123 123 @line_magic
124 124 def psearch(self, parameter_s=''):
125 125 """Search for object in namespaces by wildcard.
126 126
127 127 %psearch [options] PATTERN [OBJECT TYPE]
128 128
129 129 Note: ? can be used as a synonym for %psearch, at the beginning or at
130 130 the end: both a*? and ?a* are equivalent to '%psearch a*'. Still, the
131 131 rest of the command line must be unchanged (options come first), so
132 132 for example the following forms are equivalent
133 133
134 134 %psearch -i a* function
135 135 -i a* function?
136 136 ?-i a* function
137 137
138 138 Arguments:
139 139
140 140 PATTERN
141 141
142 142 where PATTERN is a string containing * as a wildcard similar to its
143 143 use in a shell. The pattern is matched in all namespaces on the
144 144 search path. By default objects starting with a single _ are not
145 145 matched, many IPython generated objects have a single
146 146 underscore. The default is case insensitive matching. Matching is
147 147 also done on the attributes of objects and not only on the objects
148 148 in a module.
149 149
150 150 [OBJECT TYPE]
151 151
152 152 Is the name of a python type from the types module. The name is
153 153 given in lowercase without the ending type, ex. StringType is
154 154 written string. By adding a type here only objects matching the
155 155 given type are matched. Using all here makes the pattern match all
156 156 types (this is the default).
157 157
158 158 Options:
159 159
160 160 -a: makes the pattern match even objects whose names start with a
161 161 single underscore. These names are normally omitted from the
162 162 search.
163 163
164 164 -i/-c: make the pattern case insensitive/sensitive. If neither of
165 165 these options are given, the default is read from your configuration
166 166 file, with the option ``InteractiveShell.wildcards_case_sensitive``.
167 167 If this option is not specified in your configuration file, IPython's
168 168 internal default is to do a case sensitive search.
169 169
170 170 -e/-s NAMESPACE: exclude/search a given namespace. The pattern you
171 171 specify can be searched in any of the following namespaces:
172 172 'builtin', 'user', 'user_global','internal', 'alias', where
173 173 'builtin' and 'user' are the search defaults. Note that you should
174 174 not use quotes when specifying namespaces.
175 175
176 176 'Builtin' contains the python module builtin, 'user' contains all
177 177 user data, 'alias' only contain the shell aliases and no python
178 178 objects, 'internal' contains objects used by IPython. The
179 179 'user_global' namespace is only used by embedded IPython instances,
180 180 and it contains module-level globals. You can add namespaces to the
181 181 search with -s or exclude them with -e (these options can be given
182 182 more than once).
183 183
184 184 Examples
185 185 --------
186 186 ::
187 187
188 188 %psearch a* -> objects beginning with an a
189 189 %psearch -e builtin a* -> objects NOT in the builtin space starting in a
190 190 %psearch a* function -> all functions beginning with an a
191 191 %psearch re.e* -> objects beginning with an e in module re
192 192 %psearch r*.e* -> objects that start with e in modules starting in r
193 193 %psearch r*.* string -> all strings in modules beginning with r
194 194
195 195 Case sensitive search::
196 196
197 197 %psearch -c a* list all object beginning with lower case a
198 198
199 199 Show objects beginning with a single _::
200 200
201 201 %psearch -a _* list objects beginning with a single underscore
202 202 """
203 203 try:
204 204 parameter_s.encode('ascii')
205 205 except UnicodeEncodeError:
206 206 print 'Python identifiers can only contain ascii characters.'
207 207 return
208 208
209 209 # default namespaces to be searched
210 210 def_search = ['user_local', 'user_global', 'builtin']
211 211
212 212 # Process options/args
213 213 opts,args = self.parse_options(parameter_s,'cias:e:',list_all=True)
214 214 opt = opts.get
215 215 shell = self.shell
216 216 psearch = shell.inspector.psearch
217 217
218 218 # select case options
219 219 if opts.has_key('i'):
220 220 ignore_case = True
221 221 elif opts.has_key('c'):
222 222 ignore_case = False
223 223 else:
224 224 ignore_case = not shell.wildcards_case_sensitive
225 225
226 226 # Build list of namespaces to search from user options
227 227 def_search.extend(opt('s',[]))
228 228 ns_exclude = ns_exclude=opt('e',[])
229 229 ns_search = [nm for nm in def_search if nm not in ns_exclude]
230 230
231 231 # Call the actual search
232 232 try:
233 233 psearch(args,shell.ns_table,ns_search,
234 234 show_all=opt('a'),ignore_case=ignore_case)
235 235 except:
236 236 shell.showtraceback()
237 237
238 238 @skip_doctest
239 239 @line_magic
240 240 def who_ls(self, parameter_s=''):
241 241 """Return a sorted list of all interactive variables.
242 242
243 243 If arguments are given, only variables of types matching these
244 244 arguments are returned.
245 245
246 246 Examples
247 247 --------
248 248
249 249 Define two variables and list them with who_ls::
250 250
251 251 In [1]: alpha = 123
252 252
253 253 In [2]: beta = 'test'
254 254
255 255 In [3]: %who_ls
256 256 Out[3]: ['alpha', 'beta']
257 257
258 258 In [4]: %who_ls int
259 259 Out[4]: ['alpha']
260 260
261 261 In [5]: %who_ls str
262 262 Out[5]: ['beta']
263 263 """
264 264
265 265 user_ns = self.shell.user_ns
266 266 user_ns_hidden = self.shell.user_ns_hidden
267 267 out = [ i for i in user_ns
268 268 if not i.startswith('_') \
269 269 and not i in user_ns_hidden ]
270 270
271 271 typelist = parameter_s.split()
272 272 if typelist:
273 273 typeset = set(typelist)
274 274 out = [i for i in out if type(user_ns[i]).__name__ in typeset]
275 275
276 276 out.sort()
277 277 return out
278 278
279 279 @skip_doctest
280 280 @line_magic
281 281 def who(self, parameter_s=''):
282 282 """Print all interactive variables, with some minimal formatting.
283 283
284 284 If any arguments are given, only variables whose type matches one of
285 285 these are printed. For example::
286 286
287 287 %who function str
288 288
289 289 will only list functions and strings, excluding all other types of
290 290 variables. To find the proper type names, simply use type(var) at a
291 291 command line to see how python prints type names. For example:
292 292
293 293 ::
294 294
295 295 In [1]: type('hello')\\
296 296 Out[1]: <type 'str'>
297 297
298 298 indicates that the type name for strings is 'str'.
299 299
300 300 ``%who`` always excludes executed names loaded through your configuration
301 301 file and things which are internal to IPython.
302 302
303 303 This is deliberate, as typically you may load many modules and the
304 304 purpose of %who is to show you only what you've manually defined.
305 305
306 306 Examples
307 307 --------
308 308
309 309 Define two variables and list them with who::
310 310
311 311 In [1]: alpha = 123
312 312
313 313 In [2]: beta = 'test'
314 314
315 315 In [3]: %who
316 316 alpha beta
317 317
318 318 In [4]: %who int
319 319 alpha
320 320
321 321 In [5]: %who str
322 322 beta
323 323 """
324 324
325 325 varlist = self.who_ls(parameter_s)
326 326 if not varlist:
327 327 if parameter_s:
328 328 print 'No variables match your requested type.'
329 329 else:
330 330 print 'Interactive namespace is empty.'
331 331 return
332 332
333 333 # if we have variables, move on...
334 334 count = 0
335 335 for i in varlist:
336 336 print i+'\t',
337 337 count += 1
338 338 if count > 8:
339 339 count = 0
340 340 print
341 341 print
342 342
343 343 @skip_doctest
344 344 @line_magic
345 345 def whos(self, parameter_s=''):
346 346 """Like %who, but gives some extra information about each variable.
347 347
348 348 The same type filtering of %who can be applied here.
349 349
350 350 For all variables, the type is printed. Additionally it prints:
351 351
352 352 - For {},[],(): their length.
353 353
354 354 - For numpy arrays, a summary with shape, number of
355 355 elements, typecode and size in memory.
356 356
357 357 - Everything else: a string representation, snipping their middle if
358 358 too long.
359 359
360 360 Examples
361 361 --------
362 362
363 363 Define two variables and list them with whos::
364 364
365 365 In [1]: alpha = 123
366 366
367 367 In [2]: beta = 'test'
368 368
369 369 In [3]: %whos
370 370 Variable Type Data/Info
371 371 --------------------------------
372 372 alpha int 123
373 373 beta str test
374 374 """
375 375
376 376 varnames = self.who_ls(parameter_s)
377 377 if not varnames:
378 378 if parameter_s:
379 379 print 'No variables match your requested type.'
380 380 else:
381 381 print 'Interactive namespace is empty.'
382 382 return
383 383
384 384 # if we have variables, move on...
385 385
386 386 # for these types, show len() instead of data:
387 387 seq_types = ['dict', 'list', 'tuple']
388 388
389 389 # for numpy arrays, display summary info
390 390 ndarray_type = None
391 391 if 'numpy' in sys.modules:
392 392 try:
393 393 from numpy import ndarray
394 394 except ImportError:
395 395 pass
396 396 else:
397 397 ndarray_type = ndarray.__name__
398 398
399 399 # Find all variable names and types so we can figure out column sizes
400 400 def get_vars(i):
401 401 return self.shell.user_ns[i]
402 402
403 403 # some types are well known and can be shorter
404 404 abbrevs = {'IPython.core.macro.Macro' : 'Macro'}
405 405 def type_name(v):
406 406 tn = type(v).__name__
407 407 return abbrevs.get(tn,tn)
408 408
409 409 varlist = map(get_vars,varnames)
410 410
411 411 typelist = []
412 412 for vv in varlist:
413 413 tt = type_name(vv)
414 414
415 415 if tt=='instance':
416 416 typelist.append( abbrevs.get(str(vv.__class__),
417 417 str(vv.__class__)))
418 418 else:
419 419 typelist.append(tt)
420 420
421 421 # column labels and # of spaces as separator
422 422 varlabel = 'Variable'
423 423 typelabel = 'Type'
424 424 datalabel = 'Data/Info'
425 425 colsep = 3
426 426 # variable format strings
427 427 vformat = "{0:<{varwidth}}{1:<{typewidth}}"
428 428 aformat = "%s: %s elems, type `%s`, %s bytes"
429 429 # find the size of the columns to format the output nicely
430 430 varwidth = max(max(map(len,varnames)), len(varlabel)) + colsep
431 431 typewidth = max(max(map(len,typelist)), len(typelabel)) + colsep
432 432 # table header
433 433 print varlabel.ljust(varwidth) + typelabel.ljust(typewidth) + \
434 434 ' '+datalabel+'\n' + '-'*(varwidth+typewidth+len(datalabel)+1)
435 435 # and the table itself
436 436 kb = 1024
437 437 Mb = 1048576 # kb**2
438 438 for vname,var,vtype in zip(varnames,varlist,typelist):
439 439 print vformat.format(vname, vtype, varwidth=varwidth, typewidth=typewidth),
440 440 if vtype in seq_types:
441 441 print "n="+str(len(var))
442 442 elif vtype == ndarray_type:
443 443 vshape = str(var.shape).replace(',','').replace(' ','x')[1:-1]
444 444 if vtype==ndarray_type:
445 445 # numpy
446 446 vsize = var.size
447 447 vbytes = vsize*var.itemsize
448 448 vdtype = var.dtype
449 449
450 450 if vbytes < 100000:
451 451 print aformat % (vshape,vsize,vdtype,vbytes)
452 452 else:
453 453 print aformat % (vshape,vsize,vdtype,vbytes),
454 454 if vbytes < Mb:
455 455 print '(%s kb)' % (vbytes/kb,)
456 456 else:
457 457 print '(%s Mb)' % (vbytes/Mb,)
458 458 else:
459 459 try:
460 460 vstr = str(var)
461 461 except UnicodeEncodeError:
462 462 vstr = unicode(var).encode(DEFAULT_ENCODING,
463 463 'backslashreplace')
464 464 except:
465 465 vstr = "<object with id %d (str() failed)>" % id(var)
466 466 vstr = vstr.replace('\n','\\n')
467 467 if len(vstr) < 50:
468 468 print vstr
469 469 else:
470 470 print vstr[:25] + "<...>" + vstr[-25:]
471 471
472 472 @line_magic
473 473 def reset(self, parameter_s=''):
474 474 """Resets the namespace by removing all names defined by the user, if
475 475 called without arguments, or by removing some types of objects, such
476 476 as everything currently in IPython's In[] and Out[] containers (see
477 477 the parameters for details).
478 478
479 479 Parameters
480 480 ----------
481 481 -f : force reset without asking for confirmation.
482 482
483 483 -s : 'Soft' reset: Only clears your namespace, leaving history intact.
484 484 References to objects may be kept. By default (without this option),
485 485 we do a 'hard' reset, giving you a new session and removing all
486 486 references to objects from the current session.
487 487
488 488 in : reset input history
489 489
490 490 out : reset output history
491 491
492 492 dhist : reset directory history
493 493
494 494 array : reset only variables that are NumPy arrays
495 495
496 496 See Also
497 497 --------
498 498 magic_reset_selective : invoked as ``%reset_selective``
499 499
500 500 Examples
501 501 --------
502 502 ::
503 503
504 504 In [6]: a = 1
505 505
506 506 In [7]: a
507 507 Out[7]: 1
508 508
509 509 In [8]: 'a' in _ip.user_ns
510 510 Out[8]: True
511 511
512 512 In [9]: %reset -f
513 513
514 514 In [1]: 'a' in _ip.user_ns
515 515 Out[1]: False
516 516
517 517 In [2]: %reset -f in
518 518 Flushing input history
519 519
520 520 In [3]: %reset -f dhist in
521 521 Flushing directory history
522 522 Flushing input history
523 523
524 524 Notes
525 525 -----
526 526 Calling this magic from clients that do not implement standard input,
527 527 such as the ipython notebook interface, will reset the namespace
528 528 without confirmation.
529 529 """
530 530 opts, args = self.parse_options(parameter_s,'sf', mode='list')
531 531 if 'f' in opts:
532 532 ans = True
533 533 else:
534 534 try:
535 535 ans = self.shell.ask_yes_no(
536 536 "Once deleted, variables cannot be recovered. Proceed (y/[n])?",
537 537 default='n')
538 538 except StdinNotImplementedError:
539 539 ans = True
540 540 if not ans:
541 541 print 'Nothing done.'
542 542 return
543 543
544 544 if 's' in opts: # Soft reset
545 545 user_ns = self.shell.user_ns
546 546 for i in self.who_ls():
547 547 del(user_ns[i])
548 548 elif len(args) == 0: # Hard reset
549 549 self.shell.reset(new_session = False)
550 550
551 551 # reset in/out/dhist/array: previously extensinions/clearcmd.py
552 552 ip = self.shell
553 553 user_ns = self.shell.user_ns # local lookup, heavily used
554 554
555 555 for target in args:
556 556 target = target.lower() # make matches case insensitive
557 557 if target == 'out':
558 558 print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
559 559 self.shell.displayhook.flush()
560 560
561 561 elif target == 'in':
562 562 print "Flushing input history"
563 563 pc = self.shell.displayhook.prompt_count + 1
564 564 for n in range(1, pc):
565 565 key = '_i'+repr(n)
566 566 user_ns.pop(key,None)
567 567 user_ns.update(dict(_i=u'',_ii=u'',_iii=u''))
568 568 hm = ip.history_manager
569 569 # don't delete these, as %save and %macro depending on the
570 570 # length of these lists to be preserved
571 571 hm.input_hist_parsed[:] = [''] * pc
572 572 hm.input_hist_raw[:] = [''] * pc
573 573 # hm has internal machinery for _i,_ii,_iii, clear it out
574 574 hm._i = hm._ii = hm._iii = hm._i00 = u''
575 575
576 576 elif target == 'array':
577 577 # Support cleaning up numpy arrays
578 578 try:
579 579 from numpy import ndarray
580 580 # This must be done with items and not iteritems because
581 581 # we're going to modify the dict in-place.
582 582 for x,val in user_ns.items():
583 583 if isinstance(val,ndarray):
584 584 del user_ns[x]
585 585 except ImportError:
586 586 print "reset array only works if Numpy is available."
587 587
588 588 elif target == 'dhist':
589 589 print "Flushing directory history"
590 590 del user_ns['_dh'][:]
591 591
592 592 else:
593 593 print "Don't know how to reset ",
594 594 print target + ", please run `%reset?` for details"
595 595
596 596 gc.collect()
597 597
598 598 @line_magic
599 599 def reset_selective(self, parameter_s=''):
600 600 """Resets the namespace by removing names defined by the user.
601 601
602 602 Input/Output history are left around in case you need them.
603 603
604 604 %reset_selective [-f] regex
605 605
606 606 No action is taken if regex is not included
607 607
608 608 Options
609 609 -f : force reset without asking for confirmation.
610 610
611 611 See Also
612 612 --------
613 613 magic_reset : invoked as ``%reset``
614 614
615 615 Examples
616 616 --------
617 617
618 618 We first fully reset the namespace so your output looks identical to
619 619 this example for pedagogical reasons; in practice you do not need a
620 620 full reset::
621 621
622 622 In [1]: %reset -f
623 623
624 624 Now, with a clean namespace we can make a few variables and use
625 625 ``%reset_selective`` to only delete names that match our regexp::
626 626
627 627 In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8
628 628
629 629 In [3]: who_ls
630 630 Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']
631 631
632 632 In [4]: %reset_selective -f b[2-3]m
633 633
634 634 In [5]: who_ls
635 635 Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
636 636
637 637 In [6]: %reset_selective -f d
638 638
639 639 In [7]: who_ls
640 640 Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']
641 641
642 642 In [8]: %reset_selective -f c
643 643
644 644 In [9]: who_ls
645 645 Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']
646 646
647 647 In [10]: %reset_selective -f b
648 648
649 649 In [11]: who_ls
650 650 Out[11]: ['a']
651 651
652 652 Notes
653 653 -----
654 654 Calling this magic from clients that do not implement standard input,
655 655 such as the ipython notebook interface, will reset the namespace
656 656 without confirmation.
657 657 """
658 658
659 659 opts, regex = self.parse_options(parameter_s,'f')
660 660
661 661 if opts.has_key('f'):
662 662 ans = True
663 663 else:
664 664 try:
665 665 ans = self.shell.ask_yes_no(
666 666 "Once deleted, variables cannot be recovered. Proceed (y/[n])? ",
667 667 default='n')
668 668 except StdinNotImplementedError:
669 669 ans = True
670 670 if not ans:
671 671 print 'Nothing done.'
672 672 return
673 673 user_ns = self.shell.user_ns
674 674 if not regex:
675 675 print 'No regex pattern specified. Nothing done.'
676 676 return
677 677 else:
678 678 try:
679 679 m = re.compile(regex)
680 680 except TypeError:
681 681 raise TypeError('regex must be a string or compiled pattern')
682 682 for i in self.who_ls():
683 683 if m.search(i):
684 684 del(user_ns[i])
685 685
686 686 @line_magic
687 687 def xdel(self, parameter_s=''):
688 688 """Delete a variable, trying to clear it from anywhere that
689 689 IPython's machinery has references to it. By default, this uses
690 690 the identity of the named object in the user namespace to remove
691 691 references held under other names. The object is also removed
692 692 from the output history.
693 693
694 694 Options
695 695 -n : Delete the specified name from all namespaces, without
696 696 checking their identity.
697 697 """
698 698 opts, varname = self.parse_options(parameter_s,'n')
699 699 try:
700 700 self.shell.del_var(varname, ('n' in opts))
701 701 except (NameError, ValueError) as e:
702 702 print type(e).__name__ +": "+ str(e)
@@ -1,676 +1,676 b''
1 1 """Implementation of magic functions for interaction with the OS.
2 2
3 3 Note: this module is named 'osm' instead of 'os' to avoid a collision with the
4 4 builtin.
5 5 """
6 6 #-----------------------------------------------------------------------------
7 7 # Copyright (c) 2012 The IPython Development Team.
8 8 #
9 9 # Distributed under the terms of the Modified BSD License.
10 10 #
11 11 # The full license is in the file COPYING.txt, distributed with this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Imports
16 16 #-----------------------------------------------------------------------------
17 17
18 18 # Stdlib
19 19 import os
20 20 import re
21 21 import sys
22 22 from pprint import pformat
23 23
24 24 # Our own packages
25 25 from IPython.core import oinspect
26 26 from IPython.core import page
27 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 29 line_magic)
30 30 from IPython.testing.skipdoctest import skip_doctest
31 31 from IPython.utils.io import file_read, nlprint
32 32 from IPython.utils.path import get_py_filename, unquote_filename
33 33 from IPython.utils.process import abbrev_cwd
34 34 from IPython.utils.terminal import set_term_title
35 35 #-----------------------------------------------------------------------------
36 36 # Magic implementation classes
37 37 #-----------------------------------------------------------------------------
38 @register_magics
38 @magics_class
39 39 class OSMagics(Magics):
40 40 """Magics to interact with the underlying OS (shell-type functionality).
41 41 """
42 42
43 43 @skip_doctest
44 44 @line_magic
45 45 def alias(self, parameter_s=''):
46 46 """Define an alias for a system command.
47 47
48 48 '%alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'
49 49
50 50 Then, typing 'alias_name params' will execute the system command 'cmd
51 51 params' (from your underlying operating system).
52 52
53 53 Aliases have lower precedence than magic functions and Python normal
54 54 variables, so if 'foo' is both a Python variable and an alias, the
55 55 alias can not be executed until 'del foo' removes the Python variable.
56 56
57 57 You can use the %l specifier in an alias definition to represent the
58 58 whole line when the alias is called. For example::
59 59
60 60 In [2]: alias bracket echo "Input in brackets: <%l>"
61 61 In [3]: bracket hello world
62 62 Input in brackets: <hello world>
63 63
64 64 You can also define aliases with parameters using %s specifiers (one
65 65 per parameter)::
66 66
67 67 In [1]: alias parts echo first %s second %s
68 68 In [2]: %parts A B
69 69 first A second B
70 70 In [3]: %parts A
71 71 Incorrect number of arguments: 2 expected.
72 72 parts is an alias to: 'echo first %s second %s'
73 73
74 74 Note that %l and %s are mutually exclusive. You can only use one or
75 75 the other in your aliases.
76 76
77 77 Aliases expand Python variables just like system calls using ! or !!
78 78 do: all expressions prefixed with '$' get expanded. For details of
79 79 the semantic rules, see PEP-215:
80 80 http://www.python.org/peps/pep-0215.html. This is the library used by
81 81 IPython for variable expansion. If you want to access a true shell
82 82 variable, an extra $ is necessary to prevent its expansion by
83 83 IPython::
84 84
85 85 In [6]: alias show echo
86 86 In [7]: PATH='A Python string'
87 87 In [8]: show $PATH
88 88 A Python string
89 89 In [9]: show $$PATH
90 90 /usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
91 91
92 92 You can use the alias facility to acess all of $PATH. See the %rehash
93 93 and %rehashx functions, which automatically create aliases for the
94 94 contents of your $PATH.
95 95
96 96 If called with no parameters, %alias prints the current alias table."""
97 97
98 98 par = parameter_s.strip()
99 99 if not par:
100 100 aliases = sorted(self.shell.alias_manager.aliases)
101 101 # stored = self.shell.db.get('stored_aliases', {} )
102 102 # for k, v in stored:
103 103 # atab.append(k, v[0])
104 104
105 105 print "Total number of aliases:", len(aliases)
106 106 sys.stdout.flush()
107 107 return aliases
108 108
109 109 # Now try to define a new one
110 110 try:
111 111 alias,cmd = par.split(None, 1)
112 112 except:
113 113 print oinspect.getdoc(self.alias)
114 114 else:
115 115 self.shell.alias_manager.soft_define_alias(alias, cmd)
116 116 # end magic_alias
117 117
118 118 @line_magic
119 119 def unalias(self, parameter_s=''):
120 120 """Remove an alias"""
121 121
122 122 aname = parameter_s.strip()
123 123 self.shell.alias_manager.undefine_alias(aname)
124 124 stored = self.shell.db.get('stored_aliases', {} )
125 125 if aname in stored:
126 126 print "Removing %stored alias",aname
127 127 del stored[aname]
128 128 self.shell.db['stored_aliases'] = stored
129 129
130 130 @line_magic
131 131 def rehashx(self, parameter_s=''):
132 132 """Update the alias table with all executable files in $PATH.
133 133
134 134 This version explicitly checks that every entry in $PATH is a file
135 135 with execute access (os.X_OK), so it is much slower than %rehash.
136 136
137 137 Under Windows, it checks executability as a match against a
138 138 '|'-separated string of extensions, stored in the IPython config
139 139 variable win_exec_ext. This defaults to 'exe|com|bat'.
140 140
141 141 This function also resets the root module cache of module completer,
142 142 used on slow filesystems.
143 143 """
144 144 from IPython.core.alias import InvalidAliasError
145 145
146 146 # for the benefit of module completer in ipy_completers.py
147 147 del self.shell.db['rootmodules']
148 148
149 149 path = [os.path.abspath(os.path.expanduser(p)) for p in
150 150 os.environ.get('PATH','').split(os.pathsep)]
151 151 path = filter(os.path.isdir,path)
152 152
153 153 syscmdlist = []
154 154 # Now define isexec in a cross platform manner.
155 155 if os.name == 'posix':
156 156 isexec = lambda fname:os.path.isfile(fname) and \
157 157 os.access(fname,os.X_OK)
158 158 else:
159 159 try:
160 160 winext = os.environ['pathext'].replace(';','|').replace('.','')
161 161 except KeyError:
162 162 winext = 'exe|com|bat|py'
163 163 if 'py' not in winext:
164 164 winext += '|py'
165 165 execre = re.compile(r'(.*)\.(%s)$' % winext,re.IGNORECASE)
166 166 isexec = lambda fname:os.path.isfile(fname) and execre.match(fname)
167 167 savedir = os.getcwdu()
168 168
169 169 # Now walk the paths looking for executables to alias.
170 170 try:
171 171 # write the whole loop for posix/Windows so we don't have an if in
172 172 # the innermost part
173 173 if os.name == 'posix':
174 174 for pdir in path:
175 175 os.chdir(pdir)
176 176 for ff in os.listdir(pdir):
177 177 if isexec(ff):
178 178 try:
179 179 # Removes dots from the name since ipython
180 180 # will assume names with dots to be python.
181 181 self.shell.alias_manager.define_alias(
182 182 ff.replace('.',''), ff)
183 183 except InvalidAliasError:
184 184 pass
185 185 else:
186 186 syscmdlist.append(ff)
187 187 else:
188 188 no_alias = self.shell.alias_manager.no_alias
189 189 for pdir in path:
190 190 os.chdir(pdir)
191 191 for ff in os.listdir(pdir):
192 192 base, ext = os.path.splitext(ff)
193 193 if isexec(ff) and base.lower() not in no_alias:
194 194 if ext.lower() == '.exe':
195 195 ff = base
196 196 try:
197 197 # Removes dots from the name since ipython
198 198 # will assume names with dots to be python.
199 199 self.shell.alias_manager.define_alias(
200 200 base.lower().replace('.',''), ff)
201 201 except InvalidAliasError:
202 202 pass
203 203 syscmdlist.append(ff)
204 204 self.shell.db['syscmdlist'] = syscmdlist
205 205 finally:
206 206 os.chdir(savedir)
207 207
208 208 @skip_doctest
209 209 @line_magic
210 210 def pwd(self, parameter_s=''):
211 211 """Return the current working directory path.
212 212
213 213 Examples
214 214 --------
215 215 ::
216 216
217 217 In [9]: pwd
218 218 Out[9]: '/home/tsuser/sprint/ipython'
219 219 """
220 220 return os.getcwdu()
221 221
222 222 @skip_doctest
223 223 @line_magic
224 224 def cd(self, parameter_s=''):
225 225 """Change the current working directory.
226 226
227 227 This command automatically maintains an internal list of directories
228 228 you visit during your IPython session, in the variable _dh. The
229 229 command %dhist shows this history nicely formatted. You can also
230 230 do 'cd -<tab>' to see directory history conveniently.
231 231
232 232 Usage:
233 233
234 234 cd 'dir': changes to directory 'dir'.
235 235
236 236 cd -: changes to the last visited directory.
237 237
238 238 cd -<n>: changes to the n-th directory in the directory history.
239 239
240 240 cd --foo: change to directory that matches 'foo' in history
241 241
242 242 cd -b <bookmark_name>: jump to a bookmark set by %bookmark
243 243 (note: cd <bookmark_name> is enough if there is no
244 244 directory <bookmark_name>, but a bookmark with the name exists.)
245 245 'cd -b <tab>' allows you to tab-complete bookmark names.
246 246
247 247 Options:
248 248
249 249 -q: quiet. Do not print the working directory after the cd command is
250 250 executed. By default IPython's cd command does print this directory,
251 251 since the default prompts do not display path information.
252 252
253 253 Note that !cd doesn't work for this purpose because the shell where
254 254 !command runs is immediately discarded after executing 'command'.
255 255
256 256 Examples
257 257 --------
258 258 ::
259 259
260 260 In [10]: cd parent/child
261 261 /home/tsuser/parent/child
262 262 """
263 263
264 264 #bkms = self.shell.persist.get("bookmarks",{})
265 265
266 266 oldcwd = os.getcwdu()
267 267 numcd = re.match(r'(-)(\d+)$',parameter_s)
268 268 # jump in directory history by number
269 269 if numcd:
270 270 nn = int(numcd.group(2))
271 271 try:
272 272 ps = self.shell.user_ns['_dh'][nn]
273 273 except IndexError:
274 274 print 'The requested directory does not exist in history.'
275 275 return
276 276 else:
277 277 opts = {}
278 278 elif parameter_s.startswith('--'):
279 279 ps = None
280 280 fallback = None
281 281 pat = parameter_s[2:]
282 282 dh = self.shell.user_ns['_dh']
283 283 # first search only by basename (last component)
284 284 for ent in reversed(dh):
285 285 if pat in os.path.basename(ent) and os.path.isdir(ent):
286 286 ps = ent
287 287 break
288 288
289 289 if fallback is None and pat in ent and os.path.isdir(ent):
290 290 fallback = ent
291 291
292 292 # if we have no last part match, pick the first full path match
293 293 if ps is None:
294 294 ps = fallback
295 295
296 296 if ps is None:
297 297 print "No matching entry in directory history"
298 298 return
299 299 else:
300 300 opts = {}
301 301
302 302
303 303 else:
304 304 #turn all non-space-escaping backslashes to slashes,
305 305 # for c:\windows\directory\names\
306 306 parameter_s = re.sub(r'\\(?! )','/', parameter_s)
307 307 opts,ps = self.parse_options(parameter_s,'qb',mode='string')
308 308 # jump to previous
309 309 if ps == '-':
310 310 try:
311 311 ps = self.shell.user_ns['_dh'][-2]
312 312 except IndexError:
313 313 raise UsageError('%cd -: No previous directory to change to.')
314 314 # jump to bookmark if needed
315 315 else:
316 316 if not os.path.isdir(ps) or opts.has_key('b'):
317 317 bkms = self.shell.db.get('bookmarks', {})
318 318
319 319 if bkms.has_key(ps):
320 320 target = bkms[ps]
321 321 print '(bookmark:%s) -> %s' % (ps,target)
322 322 ps = target
323 323 else:
324 324 if opts.has_key('b'):
325 325 raise UsageError("Bookmark '%s' not found. "
326 326 "Use '%%bookmark -l' to see your bookmarks." % ps)
327 327
328 328 # strip extra quotes on Windows, because os.chdir doesn't like them
329 329 ps = unquote_filename(ps)
330 330 # at this point ps should point to the target dir
331 331 if ps:
332 332 try:
333 333 os.chdir(os.path.expanduser(ps))
334 334 if hasattr(self.shell, 'term_title') and self.shell.term_title:
335 335 set_term_title('IPython: ' + abbrev_cwd())
336 336 except OSError:
337 337 print sys.exc_info()[1]
338 338 else:
339 339 cwd = os.getcwdu()
340 340 dhist = self.shell.user_ns['_dh']
341 341 if oldcwd != cwd:
342 342 dhist.append(cwd)
343 343 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
344 344
345 345 else:
346 346 os.chdir(self.shell.home_dir)
347 347 if hasattr(self.shell, 'term_title') and self.shell.term_title:
348 348 set_term_title('IPython: ' + '~')
349 349 cwd = os.getcwdu()
350 350 dhist = self.shell.user_ns['_dh']
351 351
352 352 if oldcwd != cwd:
353 353 dhist.append(cwd)
354 354 self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
355 355 if not 'q' in opts and self.shell.user_ns['_dh']:
356 356 print self.shell.user_ns['_dh'][-1]
357 357
358 358
359 359 @line_magic
360 360 def env(self, parameter_s=''):
361 361 """List environment variables."""
362 362
363 363 return dict(os.environ)
364 364
365 365 @line_magic
366 366 def pushd(self, parameter_s=''):
367 367 """Place the current dir on stack and change directory.
368 368
369 369 Usage:\\
370 370 %pushd ['dirname']
371 371 """
372 372
373 373 dir_s = self.shell.dir_stack
374 374 tgt = os.path.expanduser(unquote_filename(parameter_s))
375 375 cwd = os.getcwdu().replace(self.shell.home_dir,'~')
376 376 if tgt:
377 377 self.cd(parameter_s)
378 378 dir_s.insert(0,cwd)
379 379 return self.shell.magic('dirs')
380 380
381 381 @line_magic
382 382 def popd(self, parameter_s=''):
383 383 """Change to directory popped off the top of the stack.
384 384 """
385 385 if not self.shell.dir_stack:
386 386 raise UsageError("%popd on empty stack")
387 387 top = self.shell.dir_stack.pop(0)
388 388 self.cd(top)
389 389 print "popd ->",top
390 390
391 391 @line_magic
392 392 def dirs(self, parameter_s=''):
393 393 """Return the current directory stack."""
394 394
395 395 return self.shell.dir_stack
396 396
397 397 @line_magic
398 398 def dhist(self, parameter_s=''):
399 399 """Print your history of visited directories.
400 400
401 401 %dhist -> print full history\\
402 402 %dhist n -> print last n entries only\\
403 403 %dhist n1 n2 -> print entries between n1 and n2 (n1 not included)\\
404 404
405 405 This history is automatically maintained by the %cd command, and
406 406 always available as the global list variable _dh. You can use %cd -<n>
407 407 to go to directory number <n>.
408 408
409 409 Note that most of time, you should view directory history by entering
410 410 cd -<TAB>.
411 411
412 412 """
413 413
414 414 dh = self.shell.user_ns['_dh']
415 415 if parameter_s:
416 416 try:
417 417 args = map(int,parameter_s.split())
418 418 except:
419 419 self.arg_err(self.dhist)
420 420 return
421 421 if len(args) == 1:
422 422 ini,fin = max(len(dh)-(args[0]),0),len(dh)
423 423 elif len(args) == 2:
424 424 ini,fin = args
425 425 else:
426 426 self.arg_err(self.dhist)
427 427 return
428 428 else:
429 429 ini,fin = 0,len(dh)
430 430 nlprint(dh,
431 431 header = 'Directory history (kept in _dh)',
432 432 start=ini,stop=fin)
433 433
434 434 @skip_doctest
435 435 @line_magic
436 436 def sc(self, parameter_s=''):
437 437 """Shell capture - execute a shell command and capture its output.
438 438
439 439 DEPRECATED. Suboptimal, retained for backwards compatibility.
440 440
441 441 You should use the form 'var = !command' instead. Example:
442 442
443 443 "%sc -l myfiles = ls ~" should now be written as
444 444
445 445 "myfiles = !ls ~"
446 446
447 447 myfiles.s, myfiles.l and myfiles.n still apply as documented
448 448 below.
449 449
450 450 --
451 451 %sc [options] varname=command
452 452
453 453 IPython will run the given command using commands.getoutput(), and
454 454 will then update the user's interactive namespace with a variable
455 455 called varname, containing the value of the call. Your command can
456 456 contain shell wildcards, pipes, etc.
457 457
458 458 The '=' sign in the syntax is mandatory, and the variable name you
459 459 supply must follow Python's standard conventions for valid names.
460 460
461 461 (A special format without variable name exists for internal use)
462 462
463 463 Options:
464 464
465 465 -l: list output. Split the output on newlines into a list before
466 466 assigning it to the given variable. By default the output is stored
467 467 as a single string.
468 468
469 469 -v: verbose. Print the contents of the variable.
470 470
471 471 In most cases you should not need to split as a list, because the
472 472 returned value is a special type of string which can automatically
473 473 provide its contents either as a list (split on newlines) or as a
474 474 space-separated string. These are convenient, respectively, either
475 475 for sequential processing or to be passed to a shell command.
476 476
477 477 For example::
478 478
479 479 # Capture into variable a
480 480 In [1]: sc a=ls *py
481 481
482 482 # a is a string with embedded newlines
483 483 In [2]: a
484 484 Out[2]: 'setup.py\\nwin32_manual_post_install.py'
485 485
486 486 # which can be seen as a list:
487 487 In [3]: a.l
488 488 Out[3]: ['setup.py', 'win32_manual_post_install.py']
489 489
490 490 # or as a whitespace-separated string:
491 491 In [4]: a.s
492 492 Out[4]: 'setup.py win32_manual_post_install.py'
493 493
494 494 # a.s is useful to pass as a single command line:
495 495 In [5]: !wc -l $a.s
496 496 146 setup.py
497 497 130 win32_manual_post_install.py
498 498 276 total
499 499
500 500 # while the list form is useful to loop over:
501 501 In [6]: for f in a.l:
502 502 ...: !wc -l $f
503 503 ...:
504 504 146 setup.py
505 505 130 win32_manual_post_install.py
506 506
507 507 Similarly, the lists returned by the -l option are also special, in
508 508 the sense that you can equally invoke the .s attribute on them to
509 509 automatically get a whitespace-separated string from their contents::
510 510
511 511 In [7]: sc -l b=ls *py
512 512
513 513 In [8]: b
514 514 Out[8]: ['setup.py', 'win32_manual_post_install.py']
515 515
516 516 In [9]: b.s
517 517 Out[9]: 'setup.py win32_manual_post_install.py'
518 518
519 519 In summary, both the lists and strings used for output capture have
520 520 the following special attributes::
521 521
522 522 .l (or .list) : value as list.
523 523 .n (or .nlstr): value as newline-separated string.
524 524 .s (or .spstr): value as space-separated string.
525 525 """
526 526
527 527 opts,args = self.parse_options(parameter_s,'lv')
528 528 # Try to get a variable name and command to run
529 529 try:
530 530 # the variable name must be obtained from the parse_options
531 531 # output, which uses shlex.split to strip options out.
532 532 var,_ = args.split('=',1)
533 533 var = var.strip()
534 534 # But the command has to be extracted from the original input
535 535 # parameter_s, not on what parse_options returns, to avoid the
536 536 # quote stripping which shlex.split performs on it.
537 537 _,cmd = parameter_s.split('=',1)
538 538 except ValueError:
539 539 var,cmd = '',''
540 540 # If all looks ok, proceed
541 541 split = 'l' in opts
542 542 out = self.shell.getoutput(cmd, split=split)
543 543 if opts.has_key('v'):
544 544 print '%s ==\n%s' % (var,pformat(out))
545 545 if var:
546 546 self.shell.user_ns.update({var:out})
547 547 else:
548 548 return out
549 549
550 550 @line_magic
551 551 def sx(self, parameter_s=''):
552 552 """Shell execute - run a shell command and capture its output.
553 553
554 554 %sx command
555 555
556 556 IPython will run the given command using commands.getoutput(), and
557 557 return the result formatted as a list (split on '\\n'). Since the
558 558 output is _returned_, it will be stored in ipython's regular output
559 559 cache Out[N] and in the '_N' automatic variables.
560 560
561 561 Notes:
562 562
563 563 1) If an input line begins with '!!', then %sx is automatically
564 564 invoked. That is, while::
565 565
566 566 !ls
567 567
568 568 causes ipython to simply issue system('ls'), typing::
569 569
570 570 !!ls
571 571
572 572 is a shorthand equivalent to::
573 573
574 574 %sx ls
575 575
576 576 2) %sx differs from %sc in that %sx automatically splits into a list,
577 577 like '%sc -l'. The reason for this is to make it as easy as possible
578 578 to process line-oriented shell output via further python commands.
579 579 %sc is meant to provide much finer control, but requires more
580 580 typing.
581 581
582 582 3) Just like %sc -l, this is a list with special attributes:
583 583 ::
584 584
585 585 .l (or .list) : value as list.
586 586 .n (or .nlstr): value as newline-separated string.
587 587 .s (or .spstr): value as whitespace-separated string.
588 588
589 589 This is very useful when trying to use such lists as arguments to
590 590 system commands."""
591 591
592 592 if parameter_s:
593 593 return self.shell.getoutput(parameter_s)
594 594
595 595
596 596 @line_magic
597 597 def bookmark(self, parameter_s=''):
598 598 """Manage IPython's bookmark system.
599 599
600 600 %bookmark <name> - set bookmark to current dir
601 601 %bookmark <name> <dir> - set bookmark to <dir>
602 602 %bookmark -l - list all bookmarks
603 603 %bookmark -d <name> - remove bookmark
604 604 %bookmark -r - remove all bookmarks
605 605
606 606 You can later on access a bookmarked folder with::
607 607
608 608 %cd -b <name>
609 609
610 610 or simply '%cd <name>' if there is no directory called <name> AND
611 611 there is such a bookmark defined.
612 612
613 613 Your bookmarks persist through IPython sessions, but they are
614 614 associated with each profile."""
615 615
616 616 opts,args = self.parse_options(parameter_s,'drl',mode='list')
617 617 if len(args) > 2:
618 618 raise UsageError("%bookmark: too many arguments")
619 619
620 620 bkms = self.shell.db.get('bookmarks',{})
621 621
622 622 if opts.has_key('d'):
623 623 try:
624 624 todel = args[0]
625 625 except IndexError:
626 626 raise UsageError(
627 627 "%bookmark -d: must provide a bookmark to delete")
628 628 else:
629 629 try:
630 630 del bkms[todel]
631 631 except KeyError:
632 632 raise UsageError(
633 633 "%%bookmark -d: Can't delete bookmark '%s'" % todel)
634 634
635 635 elif opts.has_key('r'):
636 636 bkms = {}
637 637 elif opts.has_key('l'):
638 638 bks = bkms.keys()
639 639 bks.sort()
640 640 if bks:
641 641 size = max(map(len,bks))
642 642 else:
643 643 size = 0
644 644 fmt = '%-'+str(size)+'s -> %s'
645 645 print 'Current bookmarks:'
646 646 for bk in bks:
647 647 print fmt % (bk,bkms[bk])
648 648 else:
649 649 if not args:
650 650 raise UsageError("%bookmark: You must specify the bookmark name")
651 651 elif len(args)==1:
652 652 bkms[args[0]] = os.getcwdu()
653 653 elif len(args)==2:
654 654 bkms[args[0]] = args[1]
655 655 self.shell.db['bookmarks'] = bkms
656 656
657 657 @line_magic
658 658 def pycat(self, parameter_s=''):
659 659 """Show a syntax-highlighted file through a pager.
660 660
661 661 This magic is similar to the cat utility, but it will assume the file
662 662 to be Python source and will show it with syntax highlighting. """
663 663
664 664 try:
665 665 filename = get_py_filename(parameter_s)
666 666 cont = file_read(filename)
667 667 except IOError:
668 668 try:
669 669 cont = eval(parameter_s, self.shell.user_ns)
670 670 except NameError:
671 671 cont = None
672 672 if cont is None:
673 673 print "Error: no such file or variable"
674 674 return
675 675
676 676 page.page(self.shell.pycolorize(cont))
@@ -1,88 +1,88 b''
1 1 """Implementation of magic functions for matplotlib/pylab support.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2012 The IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 15 # Our own packages
16 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 18 from IPython.testing.skipdoctest import skip_doctest
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Magic implementation classes
22 22 #-----------------------------------------------------------------------------
23 23
24 @register_magics
24 @magics_class
25 25 class PylabMagics(Magics):
26 26 """Magics related to matplotlib's pylab support"""
27 27
28 28 @skip_doctest
29 29 @line_magic
30 30 def pylab(self, parameter_s=''):
31 31 """Load numpy and matplotlib to work interactively.
32 32
33 33 %pylab [GUINAME]
34 34
35 35 This function lets you activate pylab (matplotlib, numpy and
36 36 interactive support) at any point during an IPython session.
37 37
38 38 It will import at the top level numpy as np, pyplot as plt, matplotlib,
39 39 pylab and mlab, as well as all names from numpy and pylab.
40 40
41 41 If you are using the inline matplotlib backend for embedded figures,
42 42 you can adjust its behavior via the %config magic::
43 43
44 44 # enable SVG figures, necessary for SVG+XHTML export in the qtconsole
45 45 In [1]: %config InlineBackend.figure_format = 'svg'
46 46
47 47 # change the behavior of closing all figures at the end of each
48 48 # execution (cell), or allowing reuse of active figures across
49 49 # cells:
50 50 In [2]: %config InlineBackend.close_figures = False
51 51
52 52 Parameters
53 53 ----------
54 54 guiname : optional
55 55 One of the valid arguments to the %gui magic ('qt', 'wx', 'gtk',
56 56 'osx' or 'tk'). If given, the corresponding Matplotlib backend is
57 57 used, otherwise matplotlib's default (which you can override in your
58 58 matplotlib config file) is used.
59 59
60 60 Examples
61 61 --------
62 62 In this case, where the MPL default is TkAgg::
63 63
64 64 In [2]: %pylab
65 65
66 66 Welcome to pylab, a matplotlib-based Python environment.
67 67 Backend in use: TkAgg
68 68 For more information, type 'help(pylab)'.
69 69
70 70 But you can explicitly request a different backend::
71 71
72 72 In [3]: %pylab qt
73 73
74 74 Welcome to pylab, a matplotlib-based Python environment.
75 75 Backend in use: Qt4Agg
76 76 For more information, type 'help(pylab)'.
77 77 """
78 78
79 79 if Application.initialized():
80 80 app = Application.instance()
81 81 try:
82 82 import_all_status = app.pylab_import_all
83 83 except AttributeError:
84 84 import_all_status = True
85 85 else:
86 86 import_all_status = True
87 87
88 88 self.shell.enable_pylab(parameter_s, import_all=import_all_status)
@@ -1,487 +1,487 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Tests for various magic functions.
3 3
4 4 Needs to be run by nose (to make ipython session available).
5 5 """
6 6 from __future__ import absolute_import
7 7
8 8 #-----------------------------------------------------------------------------
9 9 # Imports
10 10 #-----------------------------------------------------------------------------
11 11
12 12 import io
13 13 import os
14 14 import sys
15 15 from StringIO import StringIO
16 16
17 17 import nose.tools as nt
18 18
19 19 from IPython.core import magic
20 20 from IPython.core.magics import execution
21 21 from IPython.nbformat.v3.tests.nbexamples import nb0
22 22 from IPython.nbformat import current
23 23 from IPython.testing import decorators as dec
24 24 from IPython.testing import tools as tt
25 25 from IPython.utils import py3compat
26 26 from IPython.utils.tempdir import TemporaryDirectory
27 27
28 28 #-----------------------------------------------------------------------------
29 29 # Test functions begin
30 30 #-----------------------------------------------------------------------------
31 31
32 @magic.register_magics
32 @magic.magics_class
33 33 class DummyMagics(magic.Magics): pass
34 34
35 35 def test_rehashx():
36 36 # clear up everything
37 37 _ip = get_ipython()
38 38 _ip.alias_manager.alias_table.clear()
39 39 del _ip.db['syscmdlist']
40 40
41 41 _ip.magic('rehashx')
42 42 # Practically ALL ipython development systems will have more than 10 aliases
43 43
44 44 yield (nt.assert_true, len(_ip.alias_manager.alias_table) > 10)
45 45 for key, val in _ip.alias_manager.alias_table.iteritems():
46 46 # we must strip dots from alias names
47 47 nt.assert_true('.' not in key)
48 48
49 49 # rehashx must fill up syscmdlist
50 50 scoms = _ip.db['syscmdlist']
51 51 yield (nt.assert_true, len(scoms) > 10)
52 52
53 53
54 54 def test_magic_parse_options():
55 55 """Test that we don't mangle paths when parsing magic options."""
56 56 ip = get_ipython()
57 57 path = 'c:\\x'
58 58 m = DummyMagics(ip)
59 59 opts = m.parse_options('-f %s' % path,'f:')[0]
60 60 # argv splitting is os-dependent
61 61 if os.name == 'posix':
62 62 expected = 'c:x'
63 63 else:
64 64 expected = path
65 65 nt.assert_equals(opts['f'], expected)
66 66
67 67
68 68 @dec.skip_without('sqlite3')
69 69 def doctest_hist_f():
70 70 """Test %hist -f with temporary filename.
71 71
72 72 In [9]: import tempfile
73 73
74 74 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
75 75
76 76 In [11]: %hist -nl -f $tfile 3
77 77
78 78 In [13]: import os; os.unlink(tfile)
79 79 """
80 80
81 81
82 82 @dec.skip_without('sqlite3')
83 83 def doctest_hist_r():
84 84 """Test %hist -r
85 85
86 86 XXX - This test is not recording the output correctly. For some reason, in
87 87 testing mode the raw history isn't getting populated. No idea why.
88 88 Disabling the output checking for now, though at least we do run it.
89 89
90 90 In [1]: 'hist' in _ip.lsmagic()
91 91 Out[1]: True
92 92
93 93 In [2]: x=1
94 94
95 95 In [3]: %hist -rl 2
96 96 x=1 # random
97 97 %hist -r 2
98 98 """
99 99
100 100
101 101 @dec.skip_without('sqlite3')
102 102 def doctest_hist_op():
103 103 """Test %hist -op
104 104
105 105 In [1]: class b(float):
106 106 ...: pass
107 107 ...:
108 108
109 109 In [2]: class s(object):
110 110 ...: def __str__(self):
111 111 ...: return 's'
112 112 ...:
113 113
114 114 In [3]:
115 115
116 116 In [4]: class r(b):
117 117 ...: def __repr__(self):
118 118 ...: return 'r'
119 119 ...:
120 120
121 121 In [5]: class sr(s,r): pass
122 122 ...:
123 123
124 124 In [6]:
125 125
126 126 In [7]: bb=b()
127 127
128 128 In [8]: ss=s()
129 129
130 130 In [9]: rr=r()
131 131
132 132 In [10]: ssrr=sr()
133 133
134 134 In [11]: 4.5
135 135 Out[11]: 4.5
136 136
137 137 In [12]: str(ss)
138 138 Out[12]: 's'
139 139
140 140 In [13]:
141 141
142 142 In [14]: %hist -op
143 143 >>> class b:
144 144 ... pass
145 145 ...
146 146 >>> class s(b):
147 147 ... def __str__(self):
148 148 ... return 's'
149 149 ...
150 150 >>>
151 151 >>> class r(b):
152 152 ... def __repr__(self):
153 153 ... return 'r'
154 154 ...
155 155 >>> class sr(s,r): pass
156 156 >>>
157 157 >>> bb=b()
158 158 >>> ss=s()
159 159 >>> rr=r()
160 160 >>> ssrr=sr()
161 161 >>> 4.5
162 162 4.5
163 163 >>> str(ss)
164 164 's'
165 165 >>>
166 166 """
167 167
168 168
169 169 @dec.skip_without('sqlite3')
170 170 def test_macro():
171 171 ip = get_ipython()
172 172 ip.history_manager.reset() # Clear any existing history.
173 173 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
174 174 for i, cmd in enumerate(cmds, start=1):
175 175 ip.history_manager.store_inputs(i, cmd)
176 176 ip.magic("macro test 1-3")
177 177 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
178 178
179 179 # List macros.
180 180 assert "test" in ip.magic("macro")
181 181
182 182
183 183 @dec.skip_without('sqlite3')
184 184 def test_macro_run():
185 185 """Test that we can run a multi-line macro successfully."""
186 186 ip = get_ipython()
187 187 ip.history_manager.reset()
188 188 cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"),
189 189 "%macro test 2-3"]
190 190 for cmd in cmds:
191 191 ip.run_cell(cmd, store_history=True)
192 192 nt.assert_equal(ip.user_ns["test"].value,
193 193 py3compat.doctest_refactor_print("a+=1\nprint a\n"))
194 194 with tt.AssertPrints("12"):
195 195 ip.run_cell("test")
196 196 with tt.AssertPrints("13"):
197 197 ip.run_cell("test")
198 198
199 199
200 200 @dec.skipif_not_numpy
201 201 def test_numpy_reset_array_undec():
202 202 "Test '%reset array' functionality"
203 203 _ip.ex('import numpy as np')
204 204 _ip.ex('a = np.empty(2)')
205 205 yield (nt.assert_true, 'a' in _ip.user_ns)
206 206 _ip.magic('reset -f array')
207 207 yield (nt.assert_false, 'a' in _ip.user_ns)
208 208
209 209 def test_reset_out():
210 210 "Test '%reset out' magic"
211 211 _ip.run_cell("parrot = 'dead'", store_history=True)
212 212 # test '%reset -f out', make an Out prompt
213 213 _ip.run_cell("parrot", store_history=True)
214 214 nt.assert_true('dead' in [_ip.user_ns[x] for x in '_','__','___'])
215 215 _ip.magic('reset -f out')
216 216 nt.assert_false('dead' in [_ip.user_ns[x] for x in '_','__','___'])
217 217 nt.assert_true(len(_ip.user_ns['Out']) == 0)
218 218
219 219 def test_reset_in():
220 220 "Test '%reset in' magic"
221 221 # test '%reset -f in'
222 222 _ip.run_cell("parrot", store_history=True)
223 223 nt.assert_true('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
224 224 _ip.magic('%reset -f in')
225 225 nt.assert_false('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
226 226 nt.assert_true(len(set(_ip.user_ns['In'])) == 1)
227 227
228 228 def test_reset_dhist():
229 229 "Test '%reset dhist' magic"
230 230 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
231 231 _ip.magic('cd ' + os.path.dirname(nt.__file__))
232 232 _ip.magic('cd -')
233 233 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
234 234 _ip.magic('reset -f dhist')
235 235 nt.assert_true(len(_ip.user_ns['_dh']) == 0)
236 236 _ip.run_cell("_dh = [d for d in tmp]") #restore
237 237
238 238 def test_reset_in_length():
239 239 "Test that '%reset in' preserves In[] length"
240 240 _ip.run_cell("print 'foo'")
241 241 _ip.run_cell("reset -f in")
242 242 nt.assert_true(len(_ip.user_ns['In']) == _ip.displayhook.prompt_count+1)
243 243
244 244 def test_time():
245 245 _ip.magic('time None')
246 246
247 247 def test_tb_syntaxerror():
248 248 """test %tb after a SyntaxError"""
249 249 ip = get_ipython()
250 250 ip.run_cell("for")
251 251
252 252 # trap and validate stdout
253 253 save_stdout = sys.stdout
254 254 try:
255 255 sys.stdout = StringIO()
256 256 ip.run_cell("%tb")
257 257 out = sys.stdout.getvalue()
258 258 finally:
259 259 sys.stdout = save_stdout
260 260 # trim output, and only check the last line
261 261 last_line = out.rstrip().splitlines()[-1].strip()
262 262 nt.assert_equals(last_line, "SyntaxError: invalid syntax")
263 263
264 264
265 265 @py3compat.doctest_refactor_print
266 266 def doctest_time():
267 267 """
268 268 In [10]: %time None
269 269 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
270 270 Wall time: 0.00 s
271 271
272 272 In [11]: def f(kmjy):
273 273 ....: %time print 2*kmjy
274 274
275 275 In [12]: f(3)
276 276 6
277 277 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
278 278 Wall time: 0.00 s
279 279 """
280 280
281 281
282 282 def test_doctest_mode():
283 283 "Toggle doctest_mode twice, it should be a no-op and run without error"
284 284 _ip.magic('doctest_mode')
285 285 _ip.magic('doctest_mode')
286 286
287 287
288 288 def test_parse_options():
289 289 """Tests for basic options parsing in magics."""
290 290 # These are only the most minimal of tests, more should be added later. At
291 291 # the very least we check that basic text/unicode calls work OK.
292 292 m = DummyMagics(_ip)
293 293 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
294 294 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
295 295
296 296
297 297 def test_dirops():
298 298 """Test various directory handling operations."""
299 299 # curpath = lambda :os.path.splitdrive(os.getcwdu())[1].replace('\\','/')
300 300 curpath = os.getcwdu
301 301 startdir = os.getcwdu()
302 302 ipdir = os.path.realpath(_ip.ipython_dir)
303 303 try:
304 304 _ip.magic('cd "%s"' % ipdir)
305 305 nt.assert_equal(curpath(), ipdir)
306 306 _ip.magic('cd -')
307 307 nt.assert_equal(curpath(), startdir)
308 308 _ip.magic('pushd "%s"' % ipdir)
309 309 nt.assert_equal(curpath(), ipdir)
310 310 _ip.magic('popd')
311 311 nt.assert_equal(curpath(), startdir)
312 312 finally:
313 313 os.chdir(startdir)
314 314
315 315
316 316 def test_xmode():
317 317 # Calling xmode three times should be a no-op
318 318 xmode = _ip.InteractiveTB.mode
319 319 for i in range(3):
320 320 _ip.magic("xmode")
321 321 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
322 322
323 323 def test_reset_hard():
324 324 monitor = []
325 325 class A(object):
326 326 def __del__(self):
327 327 monitor.append(1)
328 328 def __repr__(self):
329 329 return "<A instance>"
330 330
331 331 _ip.user_ns["a"] = A()
332 332 _ip.run_cell("a")
333 333
334 334 nt.assert_equal(monitor, [])
335 335 _ip.magic("reset -f")
336 336 nt.assert_equal(monitor, [1])
337 337
338 338 class TestXdel(tt.TempFileMixin):
339 339 def test_xdel(self):
340 340 """Test that references from %run are cleared by xdel."""
341 341 src = ("class A(object):\n"
342 342 " monitor = []\n"
343 343 " def __del__(self):\n"
344 344 " self.monitor.append(1)\n"
345 345 "a = A()\n")
346 346 self.mktmp(src)
347 347 # %run creates some hidden references...
348 348 _ip.magic("run %s" % self.fname)
349 349 # ... as does the displayhook.
350 350 _ip.run_cell("a")
351 351
352 352 monitor = _ip.user_ns["A"].monitor
353 353 nt.assert_equal(monitor, [])
354 354
355 355 _ip.magic("xdel a")
356 356
357 357 # Check that a's __del__ method has been called.
358 358 nt.assert_equal(monitor, [1])
359 359
360 360 def doctest_who():
361 361 """doctest for %who
362 362
363 363 In [1]: %reset -f
364 364
365 365 In [2]: alpha = 123
366 366
367 367 In [3]: beta = 'beta'
368 368
369 369 In [4]: %who int
370 370 alpha
371 371
372 372 In [5]: %who str
373 373 beta
374 374
375 375 In [6]: %whos
376 376 Variable Type Data/Info
377 377 ----------------------------
378 378 alpha int 123
379 379 beta str beta
380 380
381 381 In [7]: %who_ls
382 382 Out[7]: ['alpha', 'beta']
383 383 """
384 384
385 385 def test_whos():
386 386 """Check that whos is protected against objects where repr() fails."""
387 387 class A(object):
388 388 def __repr__(self):
389 389 raise Exception()
390 390 _ip.user_ns['a'] = A()
391 391 _ip.magic("whos")
392 392
393 393 @py3compat.u_format
394 394 def doctest_precision():
395 395 """doctest for %precision
396 396
397 397 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
398 398
399 399 In [2]: %precision 5
400 400 Out[2]: {u}'%.5f'
401 401
402 402 In [3]: f.float_format
403 403 Out[3]: {u}'%.5f'
404 404
405 405 In [4]: %precision %e
406 406 Out[4]: {u}'%e'
407 407
408 408 In [5]: f(3.1415927)
409 409 Out[5]: {u}'3.141593e+00'
410 410 """
411 411
412 412 def test_psearch():
413 413 with tt.AssertPrints("dict.fromkeys"):
414 414 _ip.run_cell("dict.fr*?")
415 415
416 416 def test_timeit_shlex():
417 417 """test shlex issues with timeit (#1109)"""
418 418 _ip.ex("def f(*a,**kw): pass")
419 419 _ip.magic('timeit -n1 "this is a bug".count(" ")')
420 420 _ip.magic('timeit -r1 -n1 f(" ", 1)')
421 421 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
422 422 _ip.magic('timeit -r1 -n1 ("a " + "b")')
423 423 _ip.magic('timeit -r1 -n1 f("a " + "b")')
424 424 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
425 425
426 426
427 427 def test_timeit_arguments():
428 428 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
429 429 _ip.magic("timeit ('#')")
430 430
431 431
432 432 @dec.skipif(execution.profile is None)
433 433 def test_prun_quotes():
434 434 "Test that prun does not clobber string escapes (GH #1302)"
435 435 _ip.magic("prun -q x = '\t'")
436 436 nt.assert_equal(_ip.user_ns['x'], '\t')
437 437
438 438 def test_extension():
439 439 tmpdir = TemporaryDirectory()
440 440 orig_ipython_dir = _ip.ipython_dir
441 441 try:
442 442 _ip.ipython_dir = tmpdir.name
443 443 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
444 444 url = os.path.join(os.path.dirname(__file__), "daft_extension.py")
445 445 _ip.magic("install_ext %s" % url)
446 446 _ip.user_ns.pop('arq', None)
447 447 _ip.magic("load_ext daft_extension")
448 448 tt.assert_equal(_ip.user_ns['arq'], 185)
449 449 _ip.magic("unload_ext daft_extension")
450 450 assert 'arq' not in _ip.user_ns
451 451 finally:
452 452 _ip.ipython_dir = orig_ipython_dir
453 453
454 454 def test_notebook_export_json():
455 455 with TemporaryDirectory() as td:
456 456 outfile = os.path.join(td, "nb.ipynb")
457 457 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
458 458 _ip.magic("notebook -e %s" % outfile)
459 459
460 460 def test_notebook_export_py():
461 461 with TemporaryDirectory() as td:
462 462 outfile = os.path.join(td, "nb.py")
463 463 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
464 464 _ip.magic("notebook -e %s" % outfile)
465 465
466 466 def test_notebook_reformat_py():
467 467 with TemporaryDirectory() as td:
468 468 infile = os.path.join(td, "nb.ipynb")
469 469 with io.open(infile, 'w', encoding='utf-8') as f:
470 470 current.write(nb0, f, 'json')
471 471
472 472 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
473 473 _ip.magic("notebook -f py %s" % infile)
474 474
475 475 def test_notebook_reformat_json():
476 476 with TemporaryDirectory() as td:
477 477 infile = os.path.join(td, "nb.py")
478 478 with io.open(infile, 'w', encoding='utf-8') as f:
479 479 current.write(nb0, f, 'py')
480 480
481 481 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
482 482 _ip.magic("notebook -f ipynb %s" % infile)
483 483 _ip.magic("notebook -f json %s" % infile)
484 484
485 485 def test_env():
486 486 env = _ip.magic("env")
487 487 assert isinstance(env, dict), type(env)
@@ -1,536 +1,536 b''
1 1 """IPython extension to reload modules before executing user code.
2 2
3 3 ``autoreload`` reloads modules automatically before entering the execution of
4 4 code typed at the IPython prompt.
5 5
6 6 This makes for example the following workflow possible:
7 7
8 8 .. sourcecode:: ipython
9 9
10 10 In [1]: %load_ext autoreload
11 11
12 12 In [2]: %autoreload 2
13 13
14 14 In [3]: from foo import some_function
15 15
16 16 In [4]: some_function()
17 17 Out[4]: 42
18 18
19 19 In [5]: # open foo.py in an editor and change some_function to return 43
20 20
21 21 In [6]: some_function()
22 22 Out[6]: 43
23 23
24 24 The module was reloaded without reloading it explicitly, and the object
25 25 imported with ``from foo import ...`` was also updated.
26 26
27 27 Usage
28 28 =====
29 29
30 30 The following magic commands are provided:
31 31
32 32 ``%autoreload``
33 33
34 34 Reload all modules (except those excluded by ``%aimport``)
35 35 automatically now.
36 36
37 37 ``%autoreload 0``
38 38
39 39 Disable automatic reloading.
40 40
41 41 ``%autoreload 1``
42 42
43 43 Reload all modules imported with ``%aimport`` every time before
44 44 executing the Python code typed.
45 45
46 46 ``%autoreload 2``
47 47
48 48 Reload all modules (except those excluded by ``%aimport``) every
49 49 time before executing the Python code typed.
50 50
51 51 ``%aimport``
52 52
53 53 List modules which are to be automatically imported or not to be imported.
54 54
55 55 ``%aimport foo``
56 56
57 57 Import module 'foo' and mark it to be autoreloaded for ``%autoreload 1``
58 58
59 59 ``%aimport -foo``
60 60
61 61 Mark module 'foo' to not be autoreloaded.
62 62
63 63 Caveats
64 64 =======
65 65
66 66 Reloading Python modules in a reliable way is in general difficult,
67 67 and unexpected things may occur. ``%autoreload`` tries to work around
68 68 common pitfalls by replacing function code objects and parts of
69 69 classes previously in the module with new versions. This makes the
70 70 following things to work:
71 71
72 72 - Functions and classes imported via 'from xxx import foo' are upgraded
73 73 to new versions when 'xxx' is reloaded.
74 74
75 75 - Methods and properties of classes are upgraded on reload, so that
76 76 calling 'c.foo()' on an object 'c' created before the reload causes
77 77 the new code for 'foo' to be executed.
78 78
79 79 Some of the known remaining caveats are:
80 80
81 81 - Replacing code objects does not always succeed: changing a @property
82 82 in a class to an ordinary method or a method to a member variable
83 83 can cause problems (but in old objects only).
84 84
85 85 - Functions that are removed (eg. via monkey-patching) from a module
86 86 before it is reloaded are not upgraded.
87 87
88 88 - C extension modules cannot be reloaded, and so cannot be autoreloaded.
89 89 """
90 90
91 91 skip_doctest = True
92 92
93 93 #-----------------------------------------------------------------------------
94 94 # Copyright (C) 2000 Thomas Heller
95 95 # Copyright (C) 2008 Pauli Virtanen <pav@iki.fi>
96 96 # Copyright (C) 2012 The IPython Development Team
97 97 #
98 98 # Distributed under the terms of the BSD License. The full license is in
99 99 # the file COPYING, distributed as part of this software.
100 100 #-----------------------------------------------------------------------------
101 101 #
102 102 # This IPython module is written by Pauli Virtanen, based on the autoreload
103 103 # code by Thomas Heller.
104 104
105 105 #-----------------------------------------------------------------------------
106 106 # Imports
107 107 #-----------------------------------------------------------------------------
108 108 import atexit
109 109 import imp
110 110 import inspect
111 111 import os
112 112 import sys
113 113 import threading
114 114 import time
115 115 import traceback
116 116 import types
117 117 import weakref
118 118
119 119 try:
120 120 # Reload is not defined by default in Python3.
121 121 reload
122 122 except NameError:
123 123 from imp import reload
124 124
125 125 from IPython.utils import pyfile
126 126 from IPython.utils.py3compat import PY3
127 127
128 128 #------------------------------------------------------------------------------
129 129 # Autoreload functionality
130 130 #------------------------------------------------------------------------------
131 131
132 132 def _get_compiled_ext():
133 133 """Official way to get the extension of compiled files (.pyc or .pyo)"""
134 134 for ext, mode, typ in imp.get_suffixes():
135 135 if typ == imp.PY_COMPILED:
136 136 return ext
137 137
138 138
139 139 PY_COMPILED_EXT = _get_compiled_ext()
140 140
141 141
142 142 class ModuleReloader(object):
143 143 enabled = False
144 144 """Whether this reloader is enabled"""
145 145
146 146 failed = {}
147 147 """Modules that failed to reload: {module: mtime-on-failed-reload, ...}"""
148 148
149 149 modules = {}
150 150 """Modules specially marked as autoreloadable."""
151 151
152 152 skip_modules = {}
153 153 """Modules specially marked as not autoreloadable."""
154 154
155 155 check_all = True
156 156 """Autoreload all modules, not just those listed in 'modules'"""
157 157
158 158 old_objects = {}
159 159 """(module-name, name) -> weakref, for replacing old code objects"""
160 160
161 161 def mark_module_skipped(self, module_name):
162 162 """Skip reloading the named module in the future"""
163 163 try:
164 164 del self.modules[module_name]
165 165 except KeyError:
166 166 pass
167 167 self.skip_modules[module_name] = True
168 168
169 169 def mark_module_reloadable(self, module_name):
170 170 """Reload the named module in the future (if it is imported)"""
171 171 try:
172 172 del self.skip_modules[module_name]
173 173 except KeyError:
174 174 pass
175 175 self.modules[module_name] = True
176 176
177 177 def aimport_module(self, module_name):
178 178 """Import a module, and mark it reloadable
179 179
180 180 Returns
181 181 -------
182 182 top_module : module
183 183 The imported module if it is top-level, or the top-level
184 184 top_name : module
185 185 Name of top_module
186 186
187 187 """
188 188 self.mark_module_reloadable(module_name)
189 189
190 190 __import__(module_name)
191 191 top_name = module_name.split('.')[0]
192 192 top_module = sys.modules[top_name]
193 193 return top_module, top_name
194 194
195 195 def check(self, check_all=False):
196 196 """Check whether some modules need to be reloaded."""
197 197
198 198 if not self.enabled and not check_all:
199 199 return
200 200
201 201 if check_all or self.check_all:
202 202 modules = sys.modules.keys()
203 203 else:
204 204 modules = self.modules.keys()
205 205
206 206 for modname in modules:
207 207 m = sys.modules.get(modname, None)
208 208
209 209 if modname in self.skip_modules:
210 210 continue
211 211
212 212 if not hasattr(m, '__file__'):
213 213 continue
214 214
215 215 if m.__name__ == '__main__':
216 216 # we cannot reload(__main__)
217 217 continue
218 218
219 219 filename = m.__file__
220 220 path, ext = os.path.splitext(filename)
221 221
222 222 if ext.lower() == '.py':
223 223 ext = PY_COMPILED_EXT
224 224 pyc_filename = pyfile.cache_from_source(filename)
225 225 py_filename = filename
226 226 else:
227 227 pyc_filename = filename
228 228 try:
229 229 py_filename = pyfile.source_from_cache(filename)
230 230 except ValueError:
231 231 continue
232 232
233 233 try:
234 234 pymtime = os.stat(py_filename).st_mtime
235 235 if pymtime <= os.stat(pyc_filename).st_mtime:
236 236 continue
237 237 if self.failed.get(py_filename, None) == pymtime:
238 238 continue
239 239 except OSError:
240 240 continue
241 241
242 242 try:
243 243 superreload(m, reload, self.old_objects)
244 244 if py_filename in self.failed:
245 245 del self.failed[py_filename]
246 246 except:
247 247 print >> sys.stderr, "[autoreload of %s failed: %s]" % (
248 248 modname, traceback.format_exc(1))
249 249 self.failed[py_filename] = pymtime
250 250
251 251 #------------------------------------------------------------------------------
252 252 # superreload
253 253 #------------------------------------------------------------------------------
254 254
255 255 if PY3:
256 256 func_attrs = ['__code__', '__defaults__', '__doc__',
257 257 '__closure__', '__globals__', '__dict__']
258 258 else:
259 259 func_attrs = ['func_code', 'func_defaults', 'func_doc',
260 260 'func_closure', 'func_globals', 'func_dict']
261 261
262 262
263 263 def update_function(old, new):
264 264 """Upgrade the code object of a function"""
265 265 for name in func_attrs:
266 266 try:
267 267 setattr(old, name, getattr(new, name))
268 268 except (AttributeError, TypeError):
269 269 pass
270 270
271 271
272 272 def update_class(old, new):
273 273 """Replace stuff in the __dict__ of a class, and upgrade
274 274 method code objects"""
275 275 for key in old.__dict__.keys():
276 276 old_obj = getattr(old, key)
277 277
278 278 try:
279 279 new_obj = getattr(new, key)
280 280 except AttributeError:
281 281 # obsolete attribute: remove it
282 282 try:
283 283 delattr(old, key)
284 284 except (AttributeError, TypeError):
285 285 pass
286 286 continue
287 287
288 288 if update_generic(old_obj, new_obj): continue
289 289
290 290 try:
291 291 setattr(old, key, getattr(new, key))
292 292 except (AttributeError, TypeError):
293 293 pass # skip non-writable attributes
294 294
295 295
296 296 def update_property(old, new):
297 297 """Replace get/set/del functions of a property"""
298 298 update_generic(old.fdel, new.fdel)
299 299 update_generic(old.fget, new.fget)
300 300 update_generic(old.fset, new.fset)
301 301
302 302
303 303 def isinstance2(a, b, typ):
304 304 return isinstance(a, typ) and isinstance(b, typ)
305 305
306 306
307 307 UPDATE_RULES = [
308 308 (lambda a, b: isinstance2(a, b, type),
309 309 update_class),
310 310 (lambda a, b: isinstance2(a, b, types.FunctionType),
311 311 update_function),
312 312 (lambda a, b: isinstance2(a, b, property),
313 313 update_property),
314 314 ]
315 315
316 316
317 317 if PY3:
318 318 UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.MethodType),
319 319 lambda a, b: update_function(a.__func__, b.__func__)),
320 320 ])
321 321 else:
322 322 UPDATE_RULES.extend([(lambda a, b: isinstance2(a, b, types.ClassType),
323 323 update_class),
324 324 (lambda a, b: isinstance2(a, b, types.MethodType),
325 325 lambda a, b: update_function(a.im_func, b.im_func)),
326 326 ])
327 327
328 328
329 329 def update_generic(a, b):
330 330 for type_check, update in UPDATE_RULES:
331 331 if type_check(a, b):
332 332 update(a, b)
333 333 return True
334 334 return False
335 335
336 336
337 337 class StrongRef(object):
338 338 def __init__(self, obj):
339 339 self.obj = obj
340 340 def __call__(self):
341 341 return self.obj
342 342
343 343
344 344 def superreload(module, reload=reload, old_objects={}):
345 345 """Enhanced version of the builtin reload function.
346 346
347 347 superreload remembers objects previously in the module, and
348 348
349 349 - upgrades the class dictionary of every old class in the module
350 350 - upgrades the code object of every old function and method
351 351 - clears the module's namespace before reloading
352 352
353 353 """
354 354
355 355 # collect old objects in the module
356 356 for name, obj in module.__dict__.items():
357 357 if not hasattr(obj, '__module__') or obj.__module__ != module.__name__:
358 358 continue
359 359 key = (module.__name__, name)
360 360 try:
361 361 old_objects.setdefault(key, []).append(weakref.ref(obj))
362 362 except TypeError:
363 363 # weakref doesn't work for all types;
364 364 # create strong references for 'important' cases
365 365 if not PY3 and isinstance(obj, types.ClassType):
366 366 old_objects.setdefault(key, []).append(StrongRef(obj))
367 367
368 368 # reload module
369 369 try:
370 370 # clear namespace first from old cruft
371 371 old_dict = module.__dict__.copy()
372 372 old_name = module.__name__
373 373 module.__dict__.clear()
374 374 module.__dict__['__name__'] = old_name
375 375 except (TypeError, AttributeError, KeyError):
376 376 pass
377 377
378 378 try:
379 379 module = reload(module)
380 380 except:
381 381 # restore module dictionary on failed reload
382 382 module.__dict__.update(old_dict)
383 383 raise
384 384
385 385 # iterate over all objects and update functions & classes
386 386 for name, new_obj in module.__dict__.items():
387 387 key = (module.__name__, name)
388 388 if key not in old_objects: continue
389 389
390 390 new_refs = []
391 391 for old_ref in old_objects[key]:
392 392 old_obj = old_ref()
393 393 if old_obj is None: continue
394 394 new_refs.append(old_ref)
395 395 update_generic(old_obj, new_obj)
396 396
397 397 if new_refs:
398 398 old_objects[key] = new_refs
399 399 else:
400 400 del old_objects[key]
401 401
402 402 return module
403 403
404 404 #------------------------------------------------------------------------------
405 405 # IPython connectivity
406 406 #------------------------------------------------------------------------------
407 407
408 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 410 from IPython.core.plugin import Plugin
411 411
412 @register_magics
412 @magics_class
413 413 class AutoreloadMagics(Magics):
414 414 def __init__(self, *a, **kw):
415 415 super(AutoreloadMagics, self).__init__(*a, **kw)
416 416 self._reloader = ModuleReloader()
417 417 self._reloader.check_all = False
418 418
419 419 @line_magic
420 420 def autoreload(self, parameter_s=''):
421 421 r"""%autoreload => Reload modules automatically
422 422
423 423 %autoreload
424 424 Reload all modules (except those excluded by %aimport) automatically
425 425 now.
426 426
427 427 %autoreload 0
428 428 Disable automatic reloading.
429 429
430 430 %autoreload 1
431 431 Reload all modules imported with %aimport every time before executing
432 432 the Python code typed.
433 433
434 434 %autoreload 2
435 435 Reload all modules (except those excluded by %aimport) every time
436 436 before executing the Python code typed.
437 437
438 438 Reloading Python modules in a reliable way is in general
439 439 difficult, and unexpected things may occur. %autoreload tries to
440 440 work around common pitfalls by replacing function code objects and
441 441 parts of classes previously in the module with new versions. This
442 442 makes the following things to work:
443 443
444 444 - Functions and classes imported via 'from xxx import foo' are upgraded
445 445 to new versions when 'xxx' is reloaded.
446 446
447 447 - Methods and properties of classes are upgraded on reload, so that
448 448 calling 'c.foo()' on an object 'c' created before the reload causes
449 449 the new code for 'foo' to be executed.
450 450
451 451 Some of the known remaining caveats are:
452 452
453 453 - Replacing code objects does not always succeed: changing a @property
454 454 in a class to an ordinary method or a method to a member variable
455 455 can cause problems (but in old objects only).
456 456
457 457 - Functions that are removed (eg. via monkey-patching) from a module
458 458 before it is reloaded are not upgraded.
459 459
460 460 - C extension modules cannot be reloaded, and so cannot be
461 461 autoreloaded.
462 462
463 463 """
464 464 if parameter_s == '':
465 465 self._reloader.check(True)
466 466 elif parameter_s == '0':
467 467 self._reloader.enabled = False
468 468 elif parameter_s == '1':
469 469 self._reloader.check_all = False
470 470 self._reloader.enabled = True
471 471 elif parameter_s == '2':
472 472 self._reloader.check_all = True
473 473 self._reloader.enabled = True
474 474
475 475 @line_magic
476 476 def aimport(self, parameter_s='', stream=None):
477 477 """%aimport => Import modules for automatic reloading.
478 478
479 479 %aimport
480 480 List modules to automatically import and not to import.
481 481
482 482 %aimport foo
483 483 Import module 'foo' and mark it to be autoreloaded for %autoreload 1
484 484
485 485 %aimport -foo
486 486 Mark module 'foo' to not be autoreloaded for %autoreload 1
487 487 """
488 488 modname = parameter_s
489 489 if not modname:
490 490 to_reload = self._reloader.modules.keys()
491 491 to_reload.sort()
492 492 to_skip = self._reloader.skip_modules.keys()
493 493 to_skip.sort()
494 494 if stream is None:
495 495 stream = sys.stdout
496 496 if self._reloader.check_all:
497 497 stream.write("Modules to reload:\nall-except-skipped\n")
498 498 else:
499 499 stream.write("Modules to reload:\n%s\n" % ' '.join(to_reload))
500 500 stream.write("\nModules to skip:\n%s\n" % ' '.join(to_skip))
501 501 elif modname.startswith('-'):
502 502 modname = modname[1:]
503 503 self._reloader.mark_module_skipped(modname)
504 504 else:
505 505 top_module, top_name = self._reloader.aimport_module(modname)
506 506
507 507 # Inject module to user namespace
508 508 self.shell.push({top_name: top_module})
509 509
510 510 def pre_run_code_hook(self, ip):
511 511 if not self._reloader.enabled:
512 512 raise TryNext
513 513 try:
514 514 self._reloader.check()
515 515 except:
516 516 pass
517 517
518 518
519 519 class AutoreloadPlugin(Plugin):
520 520 def __init__(self, shell=None, config=None):
521 521 super(AutoreloadPlugin, self).__init__(shell=shell, config=config)
522 522 self.auto_magics = AutoreloadMagics(shell)
523 523 shell.register_magics(self.auto_magics)
524 524 shell.set_hook('pre_run_code_hook', self.auto_magics.pre_run_code_hook)
525 525
526 526
527 527 _loaded = False
528 528
529 529
530 530 def load_ipython_extension(ip):
531 531 """Load the extension in IPython."""
532 532 global _loaded
533 533 if not _loaded:
534 534 plugin = AutoreloadPlugin(shell=ip, config=ip.config)
535 535 ip.plugin_manager.register_plugin('autoreload', plugin)
536 536 _loaded = True
@@ -1,316 +1,316 b''
1 1 # encoding: utf-8
2 2 """
3 3 =============
4 4 parallelmagic
5 5 =============
6 6
7 7 Magic command interface for interactive parallel work.
8 8
9 9 Usage
10 10 =====
11 11
12 12 ``%autopx``
13 13
14 14 @AUTOPX_DOC@
15 15
16 16 ``%px``
17 17
18 18 @PX_DOC@
19 19
20 20 ``%result``
21 21
22 22 @RESULT_DOC@
23 23
24 24 """
25 25
26 26 #-----------------------------------------------------------------------------
27 27 # Copyright (C) 2008 The IPython Development Team
28 28 #
29 29 # Distributed under the terms of the BSD License. The full license is in
30 30 # the file COPYING, distributed as part of this software.
31 31 #-----------------------------------------------------------------------------
32 32
33 33 #-----------------------------------------------------------------------------
34 34 # Imports
35 35 #-----------------------------------------------------------------------------
36 36
37 37 import ast
38 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 41 from IPython.testing.skipdoctest import skip_doctest
42 42
43 43 #-----------------------------------------------------------------------------
44 44 # Definitions of magic functions for use with IPython
45 45 #-----------------------------------------------------------------------------
46 46
47 47 NO_ACTIVE_VIEW = """
48 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 53 class ParallelMagics(Magics):
54 54 """A set of magics useful when controlling a parallel IPython cluster.
55 55 """
56 56
57 57 def __init__(self, shell):
58 58 super(ParallelMagics, self).__init__(shell)
59 59 # A flag showing if autopx is activated or not
60 60 self.autopx = False
61 61
62 62 @skip_doctest
63 63 @line_magic
64 64 def result(self, parameter_s=''):
65 65 """Print the result of command i on all engines..
66 66
67 67 To use this a :class:`DirectView` instance must be created
68 68 and then activated by calling its :meth:`activate` method.
69 69
70 70 Then you can do the following::
71 71
72 72 In [23]: %result
73 73 Out[23]:
74 74 <Results List>
75 75 [0] In [6]: a = 10
76 76 [1] In [6]: a = 10
77 77
78 78 In [22]: %result 6
79 79 Out[22]:
80 80 <Results List>
81 81 [0] In [6]: a = 10
82 82 [1] In [6]: a = 10
83 83 """
84 84 if self.active_view is None:
85 85 print NO_ACTIVE_VIEW
86 86 return
87 87
88 88 try:
89 89 index = int(parameter_s)
90 90 except:
91 91 index = None
92 92 result = self.active_view.get_result(index)
93 93 return result
94 94
95 95 @skip_doctest
96 96 @line_magic
97 97 def px(self, parameter_s=''):
98 98 """Executes the given python command in parallel.
99 99
100 100 To use this a :class:`DirectView` instance must be created
101 101 and then activated by calling its :meth:`activate` method.
102 102
103 103 Then you can do the following::
104 104
105 105 In [24]: %px a = 5
106 106 Parallel execution on engine(s): all
107 107 Out[24]:
108 108 <Results List>
109 109 [0] In [7]: a = 5
110 110 [1] In [7]: a = 5
111 111 """
112 112
113 113 if self.active_view is None:
114 114 print NO_ACTIVE_VIEW
115 115 return
116 116 print "Parallel execution on engine(s): %s" % self.active_view.targets
117 117 result = self.active_view.execute(parameter_s, block=False)
118 118 if self.active_view.block:
119 119 result.get()
120 120 self._maybe_display_output(result)
121 121
122 122 @skip_doctest
123 123 @line_magic
124 124 def autopx(self, parameter_s=''):
125 125 """Toggles auto parallel mode.
126 126
127 127 To use this a :class:`DirectView` instance must be created
128 128 and then activated by calling its :meth:`activate` method. Once this
129 129 is called, all commands typed at the command line are send to
130 130 the engines to be executed in parallel. To control which engine
131 131 are used, set the ``targets`` attributed of the multiengine client
132 132 before entering ``%autopx`` mode.
133 133
134 134 Then you can do the following::
135 135
136 136 In [25]: %autopx
137 137 %autopx to enabled
138 138
139 139 In [26]: a = 10
140 140 Parallel execution on engine(s): [0,1,2,3]
141 141 In [27]: print a
142 142 Parallel execution on engine(s): [0,1,2,3]
143 143 [stdout:0] 10
144 144 [stdout:1] 10
145 145 [stdout:2] 10
146 146 [stdout:3] 10
147 147
148 148
149 149 In [27]: %autopx
150 150 %autopx disabled
151 151 """
152 152 if self.autopx:
153 153 self._disable_autopx()
154 154 else:
155 155 self._enable_autopx()
156 156
157 157 def _enable_autopx(self):
158 158 """Enable %autopx mode by saving the original run_cell and installing
159 159 pxrun_cell.
160 160 """
161 161 if self.active_view is None:
162 162 print NO_ACTIVE_VIEW
163 163 return
164 164
165 165 # override run_cell and run_code
166 166 self._original_run_cell = self.shell.run_cell
167 167 self.shell.run_cell = self.pxrun_cell
168 168 self._original_run_code = self.shell.run_code
169 169 self.shell.run_code = self.pxrun_code
170 170
171 171 self.autopx = True
172 172 print "%autopx enabled"
173 173
174 174 def _disable_autopx(self):
175 175 """Disable %autopx by restoring the original InteractiveShell.run_cell.
176 176 """
177 177 if self.autopx:
178 178 self.shell.run_cell = self._original_run_cell
179 179 self.shell.run_code = self._original_run_code
180 180 self.autopx = False
181 181 print "%autopx disabled"
182 182
183 183 def _maybe_display_output(self, result):
184 184 """Maybe display the output of a parallel result.
185 185
186 186 If self.active_view.block is True, wait for the result
187 187 and display the result. Otherwise, this is a noop.
188 188 """
189 189 if isinstance(result.stdout, basestring):
190 190 # single result
191 191 stdouts = [result.stdout.rstrip()]
192 192 else:
193 193 stdouts = [s.rstrip() for s in result.stdout]
194 194
195 195 targets = self.active_view.targets
196 196 if isinstance(targets, int):
197 197 targets = [targets]
198 198 elif targets == 'all':
199 199 targets = self.active_view.client.ids
200 200
201 201 if any(stdouts):
202 202 for eid,stdout in zip(targets, stdouts):
203 203 print '[stdout:%i]'%eid, stdout
204 204
205 205
206 206 def pxrun_cell(self, raw_cell, store_history=False, silent=False):
207 207 """drop-in replacement for InteractiveShell.run_cell.
208 208
209 209 This executes code remotely, instead of in the local namespace.
210 210
211 211 See InteractiveShell.run_cell for details.
212 212 """
213 213
214 214 if (not raw_cell) or raw_cell.isspace():
215 215 return
216 216
217 217 ipself = self.shell
218 218
219 219 with ipself.builtin_trap:
220 220 cell = ipself.prefilter_manager.prefilter_lines(raw_cell)
221 221
222 222 # Store raw and processed history
223 223 if store_history:
224 224 ipself.history_manager.store_inputs(ipself.execution_count,
225 225 cell, raw_cell)
226 226
227 227 # ipself.logger.log(cell, raw_cell)
228 228
229 229 cell_name = ipself.compile.cache(cell, ipself.execution_count)
230 230
231 231 try:
232 232 ast.parse(cell, filename=cell_name)
233 233 except (OverflowError, SyntaxError, ValueError, TypeError,
234 234 MemoryError):
235 235 # Case 1
236 236 ipself.showsyntaxerror()
237 237 ipself.execution_count += 1
238 238 return None
239 239 except NameError:
240 240 # ignore name errors, because we don't know the remote keys
241 241 pass
242 242
243 243 if store_history:
244 244 # Write output to the database. Does nothing unless
245 245 # history output logging is enabled.
246 246 ipself.history_manager.store_output(ipself.execution_count)
247 247 # Each cell is a *single* input, regardless of how many lines it has
248 248 ipself.execution_count += 1
249 249 if re.search(r'get_ipython\(\)\.magic\(u?["\']%?autopx', cell):
250 250 self._disable_autopx()
251 251 return False
252 252 else:
253 253 try:
254 254 result = self.active_view.execute(cell, silent=False, block=False)
255 255 except:
256 256 ipself.showtraceback()
257 257 return True
258 258 else:
259 259 if self.active_view.block:
260 260 try:
261 261 result.get()
262 262 except:
263 263 self.shell.showtraceback()
264 264 return True
265 265 else:
266 266 self._maybe_display_output(result)
267 267 return False
268 268
269 269 def pxrun_code(self, code_obj):
270 270 """drop-in replacement for InteractiveShell.run_code.
271 271
272 272 This executes code remotely, instead of in the local namespace.
273 273
274 274 See InteractiveShell.run_code for details.
275 275 """
276 276 ipself = self.shell
277 277 # check code object for the autopx magic
278 278 if 'get_ipython' in code_obj.co_names and 'magic' in code_obj.co_names \
279 279 and any( [ isinstance(c, basestring) and 'autopx' in c
280 280 for c in code_obj.co_consts ]):
281 281 self._disable_autopx()
282 282 return False
283 283 else:
284 284 try:
285 285 result = self.active_view.execute(code_obj, block=False)
286 286 except:
287 287 ipself.showtraceback()
288 288 return True
289 289 else:
290 290 if self.active_view.block:
291 291 try:
292 292 result.get()
293 293 except:
294 294 self.shell.showtraceback()
295 295 return True
296 296 else:
297 297 self._maybe_display_output(result)
298 298 return False
299 299
300 300
301 301 __doc__ = __doc__.replace('@AUTOPX_DOC@',
302 302 " " + ParallelMagics.autopx.__doc__)
303 303 __doc__ = __doc__.replace('@PX_DOC@',
304 304 " " + ParallelMagics.px.__doc__)
305 305 __doc__ = __doc__.replace('@RESULT_DOC@',
306 306 " " + ParallelMagics.result.__doc__)
307 307
308 308 _loaded = False
309 309
310 310
311 311 def load_ipython_extension(ip):
312 312 """Load the extension in IPython."""
313 313 global _loaded
314 314 if not _loaded:
315 315 ip.register_magics(ParallelMagics)
316 316 _loaded = True
@@ -1,218 +1,218 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 %store magic for lightweight persistence.
4 4
5 5 Stores variables, aliases and macros in IPython's database.
6 6
7 7 To automatically restore stored variables at startup, add this to your
8 8 :file:`ipython_config.py` file::
9 9
10 10 c.StoreMagic.autorestore = True
11 11 """
12 12
13 13 import inspect, os, sys, textwrap
14 14
15 15 from IPython.core.error import UsageError
16 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 18 from IPython.core.plugin import Plugin
19 19 from IPython.testing.skipdoctest import skip_doctest
20 20 from IPython.utils.traitlets import Bool, Instance
21 21
22 22
23 23 def restore_aliases(ip):
24 24 staliases = ip.db.get('stored_aliases', {})
25 25 for k,v in staliases.items():
26 26 #print "restore alias",k,v # dbg
27 27 #self.alias_table[k] = v
28 28 ip.alias_manager.define_alias(k,v)
29 29
30 30
31 31 def refresh_variables(ip):
32 32 db = ip.db
33 33 for key in db.keys('autorestore/*'):
34 34 # strip autorestore
35 35 justkey = os.path.basename(key)
36 36 try:
37 37 obj = db[key]
38 38 except KeyError:
39 39 print "Unable to restore variable '%s', ignoring (use %%store -d to forget!)" % justkey
40 40 print "The error was:", sys.exc_info()[0]
41 41 else:
42 42 #print "restored",justkey,"=",obj #dbg
43 43 ip.user_ns[justkey] = obj
44 44
45 45
46 46 def restore_dhist(ip):
47 47 ip.user_ns['_dh'] = ip.db.get('dhist',[])
48 48
49 49
50 50 def restore_data(ip):
51 51 refresh_variables(ip)
52 52 restore_aliases(ip)
53 53 restore_dhist(ip)
54 54
55 55
56 @register_magics
56 @magics_class
57 57 class StoreMagics(Magics):
58 58 """Lightweight persistence for python variables.
59 59
60 60 Provides the %store magic."""
61 61
62 62 @skip_doctest
63 63 @line_magic
64 64 def store(self, parameter_s=''):
65 65 """Lightweight persistence for python variables.
66 66
67 67 Example::
68 68
69 69 In [1]: l = ['hello',10,'world']
70 70 In [2]: %store l
71 71 In [3]: exit
72 72
73 73 (IPython session is closed and started again...)
74 74
75 75 ville@badger:~$ ipython
76 76 In [1]: l
77 77 Out[1]: ['hello', 10, 'world']
78 78
79 79 Usage:
80 80
81 81 * ``%store`` - Show list of all variables and their current
82 82 values
83 83 * ``%store spam`` - Store the *current* value of the variable spam
84 84 to disk
85 85 * ``%store -d spam`` - Remove the variable and its value from storage
86 86 * ``%store -z`` - Remove all variables from storage
87 87 * ``%store -r`` - Refresh all variables from store (delete
88 88 current vals)
89 89 * ``%store foo >a.txt`` - Store value of foo to new file a.txt
90 90 * ``%store foo >>a.txt`` - Append value of foo to file a.txt
91 91
92 92 It should be noted that if you change the value of a variable, you
93 93 need to %store it again if you want to persist the new value.
94 94
95 95 Note also that the variables will need to be pickleable; most basic
96 96 python types can be safely %store'd.
97 97
98 98 Also aliases can be %store'd across sessions.
99 99 """
100 100
101 101 opts,argsl = self.parse_options(parameter_s,'drz',mode='string')
102 102 args = argsl.split(None,1)
103 103 ip = self.shell
104 104 db = ip.db
105 105 # delete
106 106 if opts.has_key('d'):
107 107 try:
108 108 todel = args[0]
109 109 except IndexError:
110 110 raise UsageError('You must provide the variable to forget')
111 111 else:
112 112 try:
113 113 del db['autorestore/' + todel]
114 114 except:
115 115 raise UsageError("Can't delete variable '%s'" % todel)
116 116 # reset
117 117 elif opts.has_key('z'):
118 118 for k in db.keys('autorestore/*'):
119 119 del db[k]
120 120
121 121 elif opts.has_key('r'):
122 122 refresh_variables(ip)
123 123
124 124
125 125 # run without arguments -> list variables & values
126 126 elif not args:
127 127 vars = self.db.keys('autorestore/*')
128 128 vars.sort()
129 129 if vars:
130 130 size = max(map(len,vars))
131 131 else:
132 132 size = 0
133 133
134 134 print 'Stored variables and their in-db values:'
135 135 fmt = '%-'+str(size)+'s -> %s'
136 136 get = db.get
137 137 for var in vars:
138 138 justkey = os.path.basename(var)
139 139 # print 30 first characters from every var
140 140 print fmt % (justkey,repr(get(var,'<unavailable>'))[:50])
141 141
142 142 # default action - store the variable
143 143 else:
144 144 # %store foo >file.txt or >>file.txt
145 145 if len(args) > 1 and args[1].startswith('>'):
146 146 fnam = os.path.expanduser(args[1].lstrip('>').lstrip())
147 147 if args[1].startswith('>>'):
148 148 fil = open(fnam,'a')
149 149 else:
150 150 fil = open(fnam,'w')
151 151 obj = ip.ev(args[0])
152 152 print "Writing '%s' (%s) to file '%s'." % (args[0],
153 153 obj.__class__.__name__, fnam)
154 154
155 155
156 156 if not isinstance (obj,basestring):
157 157 from pprint import pprint
158 158 pprint(obj,fil)
159 159 else:
160 160 fil.write(obj)
161 161 if not obj.endswith('\n'):
162 162 fil.write('\n')
163 163
164 164 fil.close()
165 165 return
166 166
167 167 # %store foo
168 168 try:
169 169 obj = ip.user_ns[args[0]]
170 170 except KeyError:
171 171 # it might be an alias
172 172 # This needs to be refactored to use the new AliasManager stuff.
173 173 if args[0] in self.alias_manager:
174 174 name = args[0]
175 175 nargs, cmd = self.alias_manager.alias_table[ name ]
176 176 staliases = db.get('stored_aliases',{})
177 177 staliases[ name ] = cmd
178 178 db['stored_aliases'] = staliases
179 179 print "Alias stored: %s (%s)" % (name, cmd)
180 180 return
181 181 else:
182 182 raise UsageError("Unknown variable '%s'" % args[0])
183 183
184 184 else:
185 185 if isinstance(inspect.getmodule(obj), FakeModule):
186 186 print textwrap.dedent("""\
187 187 Warning:%s is %s
188 188 Proper storage of interactively declared classes (or instances
189 189 of those classes) is not possible! Only instances
190 190 of classes in real modules on file system can be %%store'd.
191 191 """ % (args[0], obj) )
192 192 return
193 193 #pickled = pickle.dumps(obj)
194 194 self.db[ 'autorestore/' + args[0] ] = obj
195 195 print "Stored '%s' (%s)" % (args[0], obj.__class__.__name__)
196 196
197 197
198 198 class StoreMagic(Plugin):
199 199 shell = Instance('IPython.core.interactiveshell.InteractiveShellABC')
200 200 autorestore = Bool(False, config=True)
201 201
202 202 def __init__(self, shell, config):
203 203 super(StoreMagic, self).__init__(shell=shell, config=config)
204 204 shell.register_magics(StoreMagics)
205 205
206 206 if self.autorestore:
207 207 restore_data(shell)
208 208
209 209
210 210 _loaded = False
211 211
212 212 def load_ipython_extension(ip):
213 213 """Load the extension in IPython."""
214 214 global _loaded
215 215 if not _loaded:
216 216 plugin = StoreMagic(shell=ip, config=ip.config)
217 217 ip.plugin_manager.register_plugin('storemagic', plugin)
218 218 _loaded = True
@@ -1,277 +1,277 b''
1 1 # encoding: utf-8
2 2 """
3 3 An embedded IPython shell.
4 4
5 5 Authors:
6 6
7 7 * Brian Granger
8 8 * Fernando Perez
9 9
10 10 Notes
11 11 -----
12 12 """
13 13
14 14 #-----------------------------------------------------------------------------
15 15 # Copyright (C) 2008-2011 The IPython Development Team
16 16 #
17 17 # Distributed under the terms of the BSD License. The full license is in
18 18 # the file COPYING, distributed as part of this software.
19 19 #-----------------------------------------------------------------------------
20 20
21 21 #-----------------------------------------------------------------------------
22 22 # Imports
23 23 #-----------------------------------------------------------------------------
24 24
25 25 from __future__ import with_statement
26 26
27 27 import sys
28 28 from contextlib import nested
29 29 import warnings
30 30
31 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 33 from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell
34 34 from IPython.frontend.terminal.ipapp import load_default_config
35 35
36 36 from IPython.utils.traitlets import Bool, CBool, Unicode
37 37 from IPython.utils.io import ask_yes_no
38 38
39 39
40 40 #-----------------------------------------------------------------------------
41 41 # Classes and functions
42 42 #-----------------------------------------------------------------------------
43 43
44 44 # This is an additional magic that is exposed in embedded shells.
45 @register_magics
45 @magics_class
46 46 class EmbeddedMagics(Magics):
47 47
48 48 @line_magic
49 49 def kill_embedded(self, parameter_s=''):
50 50 """%kill_embedded : deactivate for good the current embedded IPython.
51 51
52 52 This function (after asking for confirmation) sets an internal flag so
53 53 that an embedded IPython will never activate again. This is useful to
54 54 permanently disable a shell that is being called inside a loop: once
55 55 you've figured out what you needed from it, you may then kill it and
56 56 the program will then continue to run without the interactive shell
57 57 interfering again.
58 58 """
59 59
60 60 kill = ask_yes_no("Are you sure you want to kill this embedded instance "
61 61 "(y/n)? [y/N] ",'n')
62 62 if kill:
63 63 self.shell.embedded_active = False
64 64 print ("This embedded IPython will not reactivate anymore "
65 65 "once you exit.")
66 66
67 67
68 68 class InteractiveShellEmbed(TerminalInteractiveShell):
69 69
70 70 dummy_mode = Bool(False)
71 71 exit_msg = Unicode('')
72 72 embedded = CBool(True)
73 73 embedded_active = CBool(True)
74 74 # Like the base class display_banner is not configurable, but here it
75 75 # is True by default.
76 76 display_banner = CBool(True)
77 77
78 78 def __init__(self, config=None, ipython_dir=None, user_ns=None,
79 79 user_module=None, custom_exceptions=((),None),
80 80 usage=None, banner1=None, banner2=None,
81 81 display_banner=None, exit_msg=u'', user_global_ns=None):
82 82
83 83 if user_global_ns is not None:
84 84 warnings.warn("user_global_ns has been replaced by user_module. The\
85 85 parameter will be ignored.", DeprecationWarning)
86 86
87 87 super(InteractiveShellEmbed,self).__init__(
88 88 config=config, ipython_dir=ipython_dir, user_ns=user_ns,
89 89 user_module=user_module, custom_exceptions=custom_exceptions,
90 90 usage=usage, banner1=banner1, banner2=banner2,
91 91 display_banner=display_banner
92 92 )
93 93
94 94 self.exit_msg = exit_msg
95 95
96 96 # don't use the ipython crash handler so that user exceptions aren't
97 97 # trapped
98 98 sys.excepthook = ultratb.FormattedTB(color_scheme=self.colors,
99 99 mode=self.xmode,
100 100 call_pdb=self.pdb)
101 101
102 102 def init_sys_modules(self):
103 103 pass
104 104
105 105 def init_magics(self):
106 106 super(InteractiveShellEmbed, self).init_magics()
107 107 self.register_magics(EmbeddedMagics)
108 108
109 109 def __call__(self, header='', local_ns=None, module=None, dummy=None,
110 110 stack_depth=1, global_ns=None):
111 111 """Activate the interactive interpreter.
112 112
113 113 __call__(self,header='',local_ns=None,module=None,dummy=None) -> Start
114 114 the interpreter shell with the given local and global namespaces, and
115 115 optionally print a header string at startup.
116 116
117 117 The shell can be globally activated/deactivated using the
118 118 dummy_mode attribute. This allows you to turn off a shell used
119 119 for debugging globally.
120 120
121 121 However, *each* time you call the shell you can override the current
122 122 state of dummy_mode with the optional keyword parameter 'dummy'. For
123 123 example, if you set dummy mode on with IPShell.dummy_mode = True, you
124 124 can still have a specific call work by making it as IPShell(dummy=False).
125 125 """
126 126
127 127 # If the user has turned it off, go away
128 128 if not self.embedded_active:
129 129 return
130 130
131 131 # Normal exits from interactive mode set this flag, so the shell can't
132 132 # re-enter (it checks this variable at the start of interactive mode).
133 133 self.exit_now = False
134 134
135 135 # Allow the dummy parameter to override the global __dummy_mode
136 136 if dummy or (dummy != 0 and self.dummy_mode):
137 137 return
138 138
139 139 if self.has_readline:
140 140 self.set_readline_completer()
141 141
142 142 # self.banner is auto computed
143 143 if header:
144 144 self.old_banner2 = self.banner2
145 145 self.banner2 = self.banner2 + '\n' + header + '\n'
146 146 else:
147 147 self.old_banner2 = ''
148 148
149 149 # Call the embedding code with a stack depth of 1 so it can skip over
150 150 # our call and get the original caller's namespaces.
151 151 self.mainloop(local_ns, module, stack_depth=stack_depth, global_ns=global_ns)
152 152
153 153 self.banner2 = self.old_banner2
154 154
155 155 if self.exit_msg is not None:
156 156 print self.exit_msg
157 157
158 158 def mainloop(self, local_ns=None, module=None, stack_depth=0,
159 159 display_banner=None, global_ns=None):
160 160 """Embeds IPython into a running python program.
161 161
162 162 Input:
163 163
164 164 - header: An optional header message can be specified.
165 165
166 166 - local_ns, module: working local namespace (a dict) and module (a
167 167 module or similar object). If given as None, they are automatically
168 168 taken from the scope where the shell was called, so that
169 169 program variables become visible.
170 170
171 171 - stack_depth: specifies how many levels in the stack to go to
172 172 looking for namespaces (when local_ns or module is None). This
173 173 allows an intermediate caller to make sure that this function gets
174 174 the namespace from the intended level in the stack. By default (0)
175 175 it will get its locals and globals from the immediate caller.
176 176
177 177 Warning: it's possible to use this in a program which is being run by
178 178 IPython itself (via %run), but some funny things will happen (a few
179 179 globals get overwritten). In the future this will be cleaned up, as
180 180 there is no fundamental reason why it can't work perfectly."""
181 181
182 182 if (global_ns is not None) and (module is None):
183 183 class DummyMod(object):
184 184 """A dummy module object for embedded IPython."""
185 185 pass
186 186 warnings.warn("global_ns is deprecated, use module instead.", DeprecationWarning)
187 187 module = DummyMod()
188 188 module.__dict__ = global_ns
189 189
190 190 # Get locals and globals from caller
191 191 if (local_ns is None or module is None) and self.default_user_namespaces:
192 192 call_frame = sys._getframe(stack_depth).f_back
193 193
194 194 if local_ns is None:
195 195 local_ns = call_frame.f_locals
196 196 if module is None:
197 197 global_ns = call_frame.f_globals
198 198 module = sys.modules[global_ns['__name__']]
199 199
200 200 # Save original namespace and module so we can restore them after
201 201 # embedding; otherwise the shell doesn't shut down correctly.
202 202 orig_user_module = self.user_module
203 203 orig_user_ns = self.user_ns
204 204
205 205 # Update namespaces and fire up interpreter
206 206
207 207 # The global one is easy, we can just throw it in
208 208 if module is not None:
209 209 self.user_module = module
210 210
211 211 # But the user/local one is tricky: ipython needs it to store internal
212 212 # data, but we also need the locals. We'll throw our hidden variables
213 213 # like _ih and get_ipython() into the local namespace, but delete them
214 214 # later.
215 215 if local_ns is not None:
216 216 self.user_ns = local_ns
217 217 self.init_user_ns()
218 218
219 219 # Patch for global embedding to make sure that things don't overwrite
220 220 # user globals accidentally. Thanks to Richard <rxe@renre-europe.com>
221 221 # FIXME. Test this a bit more carefully (the if.. is new)
222 222 # N.B. This can't now ever be called. Not sure what it was for.
223 223 # And now, since it wasn't called in the previous version, I'm
224 224 # commenting out these lines so they can't be called with my new changes
225 225 # --TK, 2011-12-10
226 226 #if local_ns is None and module is None:
227 227 # self.user_global_ns.update(__main__.__dict__)
228 228
229 229 # make sure the tab-completer has the correct frame information, so it
230 230 # actually completes using the frame's locals/globals
231 231 self.set_completer_frame()
232 232
233 233 with nested(self.builtin_trap, self.display_trap):
234 234 self.interact(display_banner=display_banner)
235 235
236 236 # now, purge out the local namespace of IPython's hidden variables.
237 237 if local_ns is not None:
238 238 for name in self.user_ns_hidden:
239 239 local_ns.pop(name, None)
240 240
241 241 # Restore original namespace so shell can shut down when we exit.
242 242 self.user_module = orig_user_module
243 243 self.user_ns = orig_user_ns
244 244
245 245 _embedded_shell = None
246 246
247 247
248 248 def embed(**kwargs):
249 249 """Call this to embed IPython at the current point in your program.
250 250
251 251 The first invocation of this will create an :class:`InteractiveShellEmbed`
252 252 instance and then call it. Consecutive calls just call the already
253 253 created instance.
254 254
255 255 Here is a simple example::
256 256
257 257 from IPython import embed
258 258 a = 10
259 259 b = 20
260 260 embed('First time')
261 261 c = 30
262 262 d = 40
263 263 embed
264 264
265 265 Full customization can be done by passing a :class:`Struct` in as the
266 266 config argument.
267 267 """
268 268 config = kwargs.get('config')
269 269 header = kwargs.pop('header', u'')
270 270 if config is None:
271 271 config = load_default_config()
272 272 config.InteractiveShellEmbed = config.TerminalInteractiveShell
273 273 kwargs['config'] = config
274 274 global _embedded_shell
275 275 if _embedded_shell is None:
276 276 _embedded_shell = InteractiveShellEmbed(**kwargs)
277 277 _embedded_shell(header=header, stack_depth=2)
@@ -1,676 +1,676 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Subclass of InteractiveShell for terminal based frontends."""
3 3
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de>
6 6 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
7 7 # Copyright (C) 2008-2011 The IPython Development Team
8 8 #
9 9 # Distributed under the terms of the BSD License. The full license is in
10 10 # the file COPYING, distributed as part of this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16
17 17 import bdb
18 18 import os
19 19 import re
20 20 import sys
21 21 import textwrap
22 22
23 23 from contextlib import nested
24 24
25 25 from IPython.core.error import TryNext, UsageError
26 26 from IPython.core.usage import interactive_usage, default_banner
27 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 29 from IPython.testing.skipdoctest import skip_doctest
30 30 from IPython.utils.encoding import get_stream_enc
31 31 from IPython.utils import py3compat
32 32 from IPython.utils.terminal import toggle_set_term_title, set_term_title
33 33 from IPython.utils.process import abbrev_cwd
34 34 from IPython.utils.warn import warn, error
35 35 from IPython.utils.text import num_ini_spaces, SList
36 36 from IPython.utils.traitlets import Integer, CBool, Unicode
37 37
38 38 #-----------------------------------------------------------------------------
39 39 # Utilities
40 40 #-----------------------------------------------------------------------------
41 41
42 42 def get_default_editor():
43 43 try:
44 44 ed = os.environ['EDITOR']
45 45 except KeyError:
46 46 if os.name == 'posix':
47 47 ed = 'vi' # the only one guaranteed to be there!
48 48 else:
49 49 ed = 'notepad' # same in Windows!
50 50 return ed
51 51
52 52
53 53 def get_pasted_lines(sentinel, l_input=py3compat.input):
54 54 """ Yield pasted lines until the user enters the given sentinel value.
55 55 """
56 56 print "Pasting code; enter '%s' alone on the line to stop or use Ctrl-D." \
57 57 % sentinel
58 58 while True:
59 59 try:
60 60 l = l_input(':')
61 61 if l == sentinel:
62 62 return
63 63 else:
64 64 yield l
65 65 except EOFError:
66 66 print '<EOF>'
67 67 return
68 68
69 69
70 70 def strip_email_quotes(raw_lines):
71 71 """ Strip email quotation marks at the beginning of each line.
72 72
73 73 We don't do any more input transofrmations here because the main shell's
74 74 prefiltering handles other cases.
75 75 """
76 76 lines = [re.sub(r'^\s*(\s?>)+', '', l) for l in raw_lines]
77 77 return '\n'.join(lines) + '\n'
78 78
79 79
80 80 # These two functions are needed by the %paste/%cpaste magics. In practice
81 81 # they are basically methods (they take the shell as their first argument), but
82 82 # we leave them as standalone functions because eventually the magics
83 83 # themselves will become separate objects altogether. At that point, the
84 84 # magics will have access to the shell object, and these functions can be made
85 85 # methods of the magic object, but not of the shell.
86 86
87 87 def store_or_execute(shell, block, name):
88 88 """ Execute a block, or store it in a variable, per the user's request.
89 89 """
90 90 # Dedent and prefilter so what we store matches what is executed by
91 91 # run_cell.
92 92 b = shell.prefilter(textwrap.dedent(block))
93 93
94 94 if name:
95 95 # If storing it for further editing, run the prefilter on it
96 96 shell.user_ns[name] = SList(b.splitlines())
97 97 print "Block assigned to '%s'" % name
98 98 else:
99 99 shell.user_ns['pasted_block'] = b
100 100 shell.run_cell(b)
101 101
102 102
103 103 def rerun_pasted(shell, name='pasted_block'):
104 104 """ Rerun a previously pasted command.
105 105 """
106 106 b = shell.user_ns.get(name)
107 107
108 108 # Sanity checks
109 109 if b is None:
110 110 raise UsageError('No previous pasted block available')
111 111 if not isinstance(b, basestring):
112 112 raise UsageError(
113 113 "Variable 'pasted_block' is not a string, can't execute")
114 114
115 115 print "Re-executing '%s...' (%d chars)"% (b.split('\n',1)[0], len(b))
116 116 shell.run_cell(b)
117 117
118 118
119 119 #------------------------------------------------------------------------
120 120 # Terminal-specific magics
121 121 #------------------------------------------------------------------------
122 122
123 @register_magics
123 @magics_class
124 124 class TerminalMagics(Magics):
125 125
126 126 @line_magic
127 127 def autoindent(self, parameter_s = ''):
128 128 """Toggle autoindent on/off (if available)."""
129 129
130 130 self.shell.set_autoindent()
131 131 print "Automatic indentation is:",['OFF','ON'][self.shell.autoindent]
132 132
133 133 @skip_doctest
134 134 @line_magic
135 135 def cpaste(self, parameter_s=''):
136 136 """Paste & execute a pre-formatted code block from clipboard.
137 137
138 138 You must terminate the block with '--' (two minus-signs) or Ctrl-D
139 139 alone on the line. You can also provide your own sentinel with '%paste
140 140 -s %%' ('%%' is the new sentinel for this operation)
141 141
142 142 The block is dedented prior to execution to enable execution of method
143 143 definitions. '>' and '+' characters at the beginning of a line are
144 144 ignored, to allow pasting directly from e-mails, diff files and
145 145 doctests (the '...' continuation prompt is also stripped). The
146 146 executed block is also assigned to variable named 'pasted_block' for
147 147 later editing with '%edit pasted_block'.
148 148
149 149 You can also pass a variable name as an argument, e.g. '%cpaste foo'.
150 150 This assigns the pasted block to variable 'foo' as string, without
151 151 dedenting or executing it (preceding >>> and + is still stripped)
152 152
153 153 '%cpaste -r' re-executes the block previously entered by cpaste.
154 154
155 155 Do not be alarmed by garbled output on Windows (it's a readline bug).
156 156 Just press enter and type -- (and press enter again) and the block
157 157 will be what was just pasted.
158 158
159 159 IPython statements (magics, shell escapes) are not supported (yet).
160 160
161 161 See also
162 162 --------
163 163 paste: automatically pull code from clipboard.
164 164
165 165 Examples
166 166 --------
167 167 ::
168 168
169 169 In [8]: %cpaste
170 170 Pasting code; enter '--' alone on the line to stop.
171 171 :>>> a = ["world!", "Hello"]
172 172 :>>> print " ".join(sorted(a))
173 173 :--
174 174 Hello world!
175 175 """
176 176
177 177 opts, name = self.parse_options(parameter_s, 'rs:', mode='string')
178 178 if 'r' in opts:
179 179 rerun_pasted(self.shell)
180 180 return
181 181
182 182 sentinel = opts.get('s', '--')
183 183 block = strip_email_quotes(get_pasted_lines(sentinel))
184 184 store_or_execute(self.shell, block, name)
185 185
186 186 @line_magic
187 187 def paste(self, parameter_s=''):
188 188 """Paste & execute a pre-formatted code block from clipboard.
189 189
190 190 The text is pulled directly from the clipboard without user
191 191 intervention and printed back on the screen before execution (unless
192 192 the -q flag is given to force quiet mode).
193 193
194 194 The block is dedented prior to execution to enable execution of method
195 195 definitions. '>' and '+' characters at the beginning of a line are
196 196 ignored, to allow pasting directly from e-mails, diff files and
197 197 doctests (the '...' continuation prompt is also stripped). The
198 198 executed block is also assigned to variable named 'pasted_block' for
199 199 later editing with '%edit pasted_block'.
200 200
201 201 You can also pass a variable name as an argument, e.g. '%paste foo'.
202 202 This assigns the pasted block to variable 'foo' as string, without
203 203 dedenting or executing it (preceding >>> and + is still stripped)
204 204
205 205 Options
206 206 -------
207 207
208 208 -r: re-executes the block previously entered by cpaste.
209 209
210 210 -q: quiet mode: do not echo the pasted text back to the terminal.
211 211
212 212 IPython statements (magics, shell escapes) are not supported (yet).
213 213
214 214 See also
215 215 --------
216 216 cpaste: manually paste code into terminal until you mark its end.
217 217 """
218 218 opts, name = self.parse_options(parameter_s, 'rq', mode='string')
219 219 if 'r' in opts:
220 220 rerun_pasted(self.shell)
221 221 return
222 222 try:
223 223 text = self.shell.hooks.clipboard_get()
224 224 block = strip_email_quotes(text.splitlines())
225 225 except TryNext as clipboard_exc:
226 226 message = getattr(clipboard_exc, 'args')
227 227 if message:
228 228 error(message[0])
229 229 else:
230 230 error('Could not get text from the clipboard.')
231 231 return
232 232
233 233 # By default, echo back to terminal unless quiet mode is requested
234 234 if 'q' not in opts:
235 235 write = self.shell.write
236 236 write(self.shell.pycolorize(block))
237 237 if not block.endswith('\n'):
238 238 write('\n')
239 239 write("## -- End pasted text --\n")
240 240
241 241 store_or_execute(self.shell, block, name)
242 242
243 243 # Class-level: add a '%cls' magic only on Windows
244 244 if sys.platform == 'win32':
245 245 @line_magic
246 246 def cls(self, s):
247 247 """Clear screen.
248 248 """
249 249 os.system("cls")
250 250
251 251 #-----------------------------------------------------------------------------
252 252 # Main class
253 253 #-----------------------------------------------------------------------------
254 254
255 255 class TerminalInteractiveShell(InteractiveShell):
256 256
257 257 autoedit_syntax = CBool(False, config=True,
258 258 help="auto editing of files with syntax errors.")
259 259 banner = Unicode('')
260 260 banner1 = Unicode(default_banner, config=True,
261 261 help="""The part of the banner to be printed before the profile"""
262 262 )
263 263 banner2 = Unicode('', config=True,
264 264 help="""The part of the banner to be printed after the profile"""
265 265 )
266 266 confirm_exit = CBool(True, config=True,
267 267 help="""
268 268 Set to confirm when you try to exit IPython with an EOF (Control-D
269 269 in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
270 270 you can force a direct exit without any confirmation.""",
271 271 )
272 272 # This display_banner only controls whether or not self.show_banner()
273 273 # is called when mainloop/interact are called. The default is False
274 274 # because for the terminal based application, the banner behavior
275 275 # is controlled by Global.display_banner, which IPythonApp looks at
276 276 # to determine if *it* should call show_banner() by hand or not.
277 277 display_banner = CBool(False) # This isn't configurable!
278 278 embedded = CBool(False)
279 279 embedded_active = CBool(False)
280 280 editor = Unicode(get_default_editor(), config=True,
281 281 help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
282 282 )
283 283 pager = Unicode('less', config=True,
284 284 help="The shell program to be used for paging.")
285 285
286 286 screen_length = Integer(0, config=True,
287 287 help=
288 288 """Number of lines of your screen, used to control printing of very
289 289 long strings. Strings longer than this number of lines will be sent
290 290 through a pager instead of directly printed. The default value for
291 291 this is 0, which means IPython will auto-detect your screen size every
292 292 time it needs to print certain potentially long strings (this doesn't
293 293 change the behavior of the 'print' keyword, it's only triggered
294 294 internally). If for some reason this isn't working well (it needs
295 295 curses support), specify it yourself. Otherwise don't change the
296 296 default.""",
297 297 )
298 298 term_title = CBool(False, config=True,
299 299 help="Enable auto setting the terminal title."
300 300 )
301 301
302 302 # In the terminal, GUI control is done via PyOS_InputHook
303 303 from IPython.lib.inputhook import enable_gui
304 304 enable_gui = staticmethod(enable_gui)
305 305
306 306 def __init__(self, config=None, ipython_dir=None, profile_dir=None,
307 307 user_ns=None, user_module=None, custom_exceptions=((),None),
308 308 usage=None, banner1=None, banner2=None, display_banner=None):
309 309
310 310 super(TerminalInteractiveShell, self).__init__(
311 311 config=config, profile_dir=profile_dir, user_ns=user_ns,
312 312 user_module=user_module, custom_exceptions=custom_exceptions
313 313 )
314 314 # use os.system instead of utils.process.system by default,
315 315 # because piped system doesn't make sense in the Terminal:
316 316 self.system = self.system_raw
317 317
318 318 self.init_term_title()
319 319 self.init_usage(usage)
320 320 self.init_banner(banner1, banner2, display_banner)
321 321
322 322 #-------------------------------------------------------------------------
323 323 # Things related to the terminal
324 324 #-------------------------------------------------------------------------
325 325
326 326 @property
327 327 def usable_screen_length(self):
328 328 if self.screen_length == 0:
329 329 return 0
330 330 else:
331 331 num_lines_bot = self.separate_in.count('\n')+1
332 332 return self.screen_length - num_lines_bot
333 333
334 334 def init_term_title(self):
335 335 # Enable or disable the terminal title.
336 336 if self.term_title:
337 337 toggle_set_term_title(True)
338 338 set_term_title('IPython: ' + abbrev_cwd())
339 339 else:
340 340 toggle_set_term_title(False)
341 341
342 342 #-------------------------------------------------------------------------
343 343 # Things related to aliases
344 344 #-------------------------------------------------------------------------
345 345
346 346 def init_alias(self):
347 347 # The parent class defines aliases that can be safely used with any
348 348 # frontend.
349 349 super(TerminalInteractiveShell, self).init_alias()
350 350
351 351 # Now define aliases that only make sense on the terminal, because they
352 352 # need direct access to the console in a way that we can't emulate in
353 353 # GUI or web frontend
354 354 if os.name == 'posix':
355 355 aliases = [('clear', 'clear'), ('more', 'more'), ('less', 'less'),
356 356 ('man', 'man')]
357 357 elif os.name == 'nt':
358 358 aliases = [('cls', 'cls')]
359 359
360 360
361 361 for name, cmd in aliases:
362 362 self.alias_manager.define_alias(name, cmd)
363 363
364 364 #-------------------------------------------------------------------------
365 365 # Things related to the banner and usage
366 366 #-------------------------------------------------------------------------
367 367
368 368 def _banner1_changed(self):
369 369 self.compute_banner()
370 370
371 371 def _banner2_changed(self):
372 372 self.compute_banner()
373 373
374 374 def _term_title_changed(self, name, new_value):
375 375 self.init_term_title()
376 376
377 377 def init_banner(self, banner1, banner2, display_banner):
378 378 if banner1 is not None:
379 379 self.banner1 = banner1
380 380 if banner2 is not None:
381 381 self.banner2 = banner2
382 382 if display_banner is not None:
383 383 self.display_banner = display_banner
384 384 self.compute_banner()
385 385
386 386 def show_banner(self, banner=None):
387 387 if banner is None:
388 388 banner = self.banner
389 389 self.write(banner)
390 390
391 391 def compute_banner(self):
392 392 self.banner = self.banner1
393 393 if self.profile and self.profile != 'default':
394 394 self.banner += '\nIPython profile: %s\n' % self.profile
395 395 if self.banner2:
396 396 self.banner += '\n' + self.banner2
397 397
398 398 def init_usage(self, usage=None):
399 399 if usage is None:
400 400 self.usage = interactive_usage
401 401 else:
402 402 self.usage = usage
403 403
404 404 #-------------------------------------------------------------------------
405 405 # Mainloop and code execution logic
406 406 #-------------------------------------------------------------------------
407 407
408 408 def mainloop(self, display_banner=None):
409 409 """Start the mainloop.
410 410
411 411 If an optional banner argument is given, it will override the
412 412 internally created default banner.
413 413 """
414 414
415 415 with nested(self.builtin_trap, self.display_trap):
416 416
417 417 while 1:
418 418 try:
419 419 self.interact(display_banner=display_banner)
420 420 #self.interact_with_readline()
421 421 # XXX for testing of a readline-decoupled repl loop, call
422 422 # interact_with_readline above
423 423 break
424 424 except KeyboardInterrupt:
425 425 # this should not be necessary, but KeyboardInterrupt
426 426 # handling seems rather unpredictable...
427 427 self.write("\nKeyboardInterrupt in interact()\n")
428 428
429 429 def _replace_rlhist_multiline(self, source_raw, hlen_before_cell):
430 430 """Store multiple lines as a single entry in history"""
431 431
432 432 # do nothing without readline or disabled multiline
433 433 if not self.has_readline or not self.multiline_history:
434 434 return hlen_before_cell
435 435
436 436 # windows rl has no remove_history_item
437 437 if not hasattr(self.readline, "remove_history_item"):
438 438 return hlen_before_cell
439 439
440 440 # skip empty cells
441 441 if not source_raw.rstrip():
442 442 return hlen_before_cell
443 443
444 444 # nothing changed do nothing, e.g. when rl removes consecutive dups
445 445 hlen = self.readline.get_current_history_length()
446 446 if hlen == hlen_before_cell:
447 447 return hlen_before_cell
448 448
449 449 for i in range(hlen - hlen_before_cell):
450 450 self.readline.remove_history_item(hlen - i - 1)
451 451 stdin_encoding = get_stream_enc(sys.stdin, 'utf-8')
452 452 self.readline.add_history(py3compat.unicode_to_str(source_raw.rstrip(),
453 453 stdin_encoding))
454 454 return self.readline.get_current_history_length()
455 455
456 456 def interact(self, display_banner=None):
457 457 """Closely emulate the interactive Python console."""
458 458
459 459 # batch run -> do not interact
460 460 if self.exit_now:
461 461 return
462 462
463 463 if display_banner is None:
464 464 display_banner = self.display_banner
465 465
466 466 if isinstance(display_banner, basestring):
467 467 self.show_banner(display_banner)
468 468 elif display_banner:
469 469 self.show_banner()
470 470
471 471 more = False
472 472
473 473 if self.has_readline:
474 474 self.readline_startup_hook(self.pre_readline)
475 475 hlen_b4_cell = self.readline.get_current_history_length()
476 476 else:
477 477 hlen_b4_cell = 0
478 478 # exit_now is set by a call to %Exit or %Quit, through the
479 479 # ask_exit callback.
480 480
481 481 while not self.exit_now:
482 482 self.hooks.pre_prompt_hook()
483 483 if more:
484 484 try:
485 485 prompt = self.prompt_manager.render('in2')
486 486 except:
487 487 self.showtraceback()
488 488 if self.autoindent:
489 489 self.rl_do_indent = True
490 490
491 491 else:
492 492 try:
493 493 prompt = self.separate_in + self.prompt_manager.render('in')
494 494 except:
495 495 self.showtraceback()
496 496 try:
497 497 line = self.raw_input(prompt)
498 498 if self.exit_now:
499 499 # quick exit on sys.std[in|out] close
500 500 break
501 501 if self.autoindent:
502 502 self.rl_do_indent = False
503 503
504 504 except KeyboardInterrupt:
505 505 #double-guard against keyboardinterrupts during kbdint handling
506 506 try:
507 507 self.write('\nKeyboardInterrupt\n')
508 508 source_raw = self.input_splitter.source_raw_reset()[1]
509 509 hlen_b4_cell = \
510 510 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
511 511 more = False
512 512 except KeyboardInterrupt:
513 513 pass
514 514 except EOFError:
515 515 if self.autoindent:
516 516 self.rl_do_indent = False
517 517 if self.has_readline:
518 518 self.readline_startup_hook(None)
519 519 self.write('\n')
520 520 self.exit()
521 521 except bdb.BdbQuit:
522 522 warn('The Python debugger has exited with a BdbQuit exception.\n'
523 523 'Because of how pdb handles the stack, it is impossible\n'
524 524 'for IPython to properly format this particular exception.\n'
525 525 'IPython will resume normal operation.')
526 526 except:
527 527 # exceptions here are VERY RARE, but they can be triggered
528 528 # asynchronously by signal handlers, for example.
529 529 self.showtraceback()
530 530 else:
531 531 self.input_splitter.push(line)
532 532 more = self.input_splitter.push_accepts_more()
533 533 if (self.SyntaxTB.last_syntax_error and
534 534 self.autoedit_syntax):
535 535 self.edit_syntax_error()
536 536 if not more:
537 537 source_raw = self.input_splitter.source_raw_reset()[1]
538 538 self.run_cell(source_raw, store_history=True)
539 539 hlen_b4_cell = \
540 540 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
541 541
542 542 # Turn off the exit flag, so the mainloop can be restarted if desired
543 543 self.exit_now = False
544 544
545 545 def raw_input(self, prompt=''):
546 546 """Write a prompt and read a line.
547 547
548 548 The returned line does not include the trailing newline.
549 549 When the user enters the EOF key sequence, EOFError is raised.
550 550
551 551 Optional inputs:
552 552
553 553 - prompt(''): a string to be printed to prompt the user.
554 554
555 555 - continue_prompt(False): whether this line is the first one or a
556 556 continuation in a sequence of inputs.
557 557 """
558 558 # Code run by the user may have modified the readline completer state.
559 559 # We must ensure that our completer is back in place.
560 560
561 561 if self.has_readline:
562 562 self.set_readline_completer()
563 563
564 564 try:
565 565 line = py3compat.str_to_unicode(self.raw_input_original(prompt))
566 566 except ValueError:
567 567 warn("\n********\nYou or a %run:ed script called sys.stdin.close()"
568 568 " or sys.stdout.close()!\nExiting IPython!")
569 569 self.ask_exit()
570 570 return ""
571 571
572 572 # Try to be reasonably smart about not re-indenting pasted input more
573 573 # than necessary. We do this by trimming out the auto-indent initial
574 574 # spaces, if the user's actual input started itself with whitespace.
575 575 if self.autoindent:
576 576 if num_ini_spaces(line) > self.indent_current_nsp:
577 577 line = line[self.indent_current_nsp:]
578 578 self.indent_current_nsp = 0
579 579
580 580 return line
581 581
582 582 #-------------------------------------------------------------------------
583 583 # Methods to support auto-editing of SyntaxErrors.
584 584 #-------------------------------------------------------------------------
585 585
586 586 def edit_syntax_error(self):
587 587 """The bottom half of the syntax error handler called in the main loop.
588 588
589 589 Loop until syntax error is fixed or user cancels.
590 590 """
591 591
592 592 while self.SyntaxTB.last_syntax_error:
593 593 # copy and clear last_syntax_error
594 594 err = self.SyntaxTB.clear_err_state()
595 595 if not self._should_recompile(err):
596 596 return
597 597 try:
598 598 # may set last_syntax_error again if a SyntaxError is raised
599 599 self.safe_execfile(err.filename,self.user_ns)
600 600 except:
601 601 self.showtraceback()
602 602 else:
603 603 try:
604 604 f = open(err.filename)
605 605 try:
606 606 # This should be inside a display_trap block and I
607 607 # think it is.
608 608 sys.displayhook(f.read())
609 609 finally:
610 610 f.close()
611 611 except:
612 612 self.showtraceback()
613 613
614 614 def _should_recompile(self,e):
615 615 """Utility routine for edit_syntax_error"""
616 616
617 617 if e.filename in ('<ipython console>','<input>','<string>',
618 618 '<console>','<BackgroundJob compilation>',
619 619 None):
620 620
621 621 return False
622 622 try:
623 623 if (self.autoedit_syntax and
624 624 not self.ask_yes_no('Return to editor to correct syntax error? '
625 625 '[Y/n] ','y')):
626 626 return False
627 627 except EOFError:
628 628 return False
629 629
630 630 def int0(x):
631 631 try:
632 632 return int(x)
633 633 except TypeError:
634 634 return 0
635 635 # always pass integer line and offset values to editor hook
636 636 try:
637 637 self.hooks.fix_error_editor(e.filename,
638 638 int0(e.lineno),int0(e.offset),e.msg)
639 639 except TryNext:
640 640 warn('Could not open editor')
641 641 return False
642 642 return True
643 643
644 644 #-------------------------------------------------------------------------
645 645 # Things related to exiting
646 646 #-------------------------------------------------------------------------
647 647
648 648 def ask_exit(self):
649 649 """ Ask the shell to exit. Can be overiden and used as a callback. """
650 650 self.exit_now = True
651 651
652 652 def exit(self):
653 653 """Handle interactive exit.
654 654
655 655 This method calls the ask_exit callback."""
656 656 if self.confirm_exit:
657 657 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y'):
658 658 self.ask_exit()
659 659 else:
660 660 self.ask_exit()
661 661
662 662 #-------------------------------------------------------------------------
663 663 # Things related to magics
664 664 #-------------------------------------------------------------------------
665 665
666 666 def init_magics(self):
667 667 super(TerminalInteractiveShell, self).init_magics()
668 668 self.register_magics(TerminalMagics)
669 669
670 670 def showindentationerror(self):
671 671 super(TerminalInteractiveShell, self).showindentationerror()
672 672 print("If you want to paste code into IPython, try the "
673 673 "%paste and %cpaste magic functions.")
674 674
675 675
676 676 InteractiveShellABC.register(TerminalInteractiveShell)
General Comments 0
You need to be logged in to leave comments. Login now