##// END OF EJS Templates
Merge pull request #4153 from Carreau/timeitobj...
Thomas Kluyver -
r12758:6ee197ab merge
parent child Browse files
Show More
@@ -1,1248 +1,1288 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Implementation of execution-related magic functions.
2 """Implementation of execution-related magic functions.
3 """
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2012 The IPython Development Team.
5 # Copyright (c) 2012 The IPython Development Team.
6 #
6 #
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8 #
8 #
9 # The full license is in the file COPYING.txt, distributed with this software.
9 # The full license is in the file COPYING.txt, distributed with this software.
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 # Stdlib
16 # Stdlib
17 import __builtin__ as builtin_mod
17 import __builtin__ as builtin_mod
18 import ast
18 import ast
19 import bdb
19 import bdb
20 import os
20 import os
21 import sys
21 import sys
22 import time
22 import time
23 from StringIO import StringIO
23 from StringIO import StringIO
24
24
25 # cProfile was added in Python2.5
25 # cProfile was added in Python2.5
26 try:
26 try:
27 import cProfile as profile
27 import cProfile as profile
28 import pstats
28 import pstats
29 except ImportError:
29 except ImportError:
30 # profile isn't bundled by default in Debian for license reasons
30 # profile isn't bundled by default in Debian for license reasons
31 try:
31 try:
32 import profile, pstats
32 import profile, pstats
33 except ImportError:
33 except ImportError:
34 profile = pstats = None
34 profile = pstats = None
35
35
36 # Our own packages
36 # Our own packages
37 from IPython.core import debugger, oinspect
37 from IPython.core import debugger, oinspect
38 from IPython.core import magic_arguments
38 from IPython.core import magic_arguments
39 from IPython.core import page
39 from IPython.core import page
40 from IPython.core.error import UsageError
40 from IPython.core.error import UsageError
41 from IPython.core.macro import Macro
41 from IPython.core.macro import Macro
42 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
42 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
43 line_cell_magic, on_off, needs_local_scope)
43 line_cell_magic, on_off, needs_local_scope)
44 from IPython.testing.skipdoctest import skip_doctest
44 from IPython.testing.skipdoctest import skip_doctest
45 from IPython.utils import py3compat
45 from IPython.utils import py3compat
46 from IPython.utils.contexts import preserve_keys
46 from IPython.utils.contexts import preserve_keys
47 from IPython.utils.io import capture_output
47 from IPython.utils.io import capture_output
48 from IPython.utils.ipstruct import Struct
48 from IPython.utils.ipstruct import Struct
49 from IPython.utils.module_paths import find_mod
49 from IPython.utils.module_paths import find_mod
50 from IPython.utils.path import get_py_filename, unquote_filename, shellglob
50 from IPython.utils.path import get_py_filename, unquote_filename, shellglob
51 from IPython.utils.timing import clock, clock2
51 from IPython.utils.timing import clock, clock2
52 from IPython.utils.warn import warn, error
52 from IPython.utils.warn import warn, error
53
53
54
54
55 #-----------------------------------------------------------------------------
55 #-----------------------------------------------------------------------------
56 # Magic implementation classes
56 # Magic implementation classes
57 #-----------------------------------------------------------------------------
57 #-----------------------------------------------------------------------------
58
58
59
60 class TimeitResult(object):
61 """
62 Object returned by the timeit magic with info about the run.
63
64 Contain the following attributes :
65
66 loops: (int) number of loop done per measurement
67 repeat: (int) number of time the mesurement has been repeated
68 best: (float) best execusion time / number
69 all_runs: (list of float) execusion time of each run (in s)
70 compile_time: (float) time of statement compilation (s)
71
72 """
73
74 def __init__(self, loops, repeat, best, all_runs, compile_time, precision):
75 self.loops = loops
76 self.repeat = repeat
77 self.best = best
78 self.all_runs = all_runs
79 self.compile_time = compile_time
80 self._precision = precision
81
82 def _repr_pretty_(self, p , cycle):
83 unic = u"%d loops, best of %d: %s per loop" % (self.loops, self.repeat,
84 _format_time(self.best, self._precision))
85 p.text(u'<TimeitResult : '+unic+u'>')
86
87
88
89
59 @magics_class
90 @magics_class
60 class ExecutionMagics(Magics):
91 class ExecutionMagics(Magics):
61 """Magics related to code execution, debugging, profiling, etc.
92 """Magics related to code execution, debugging, profiling, etc.
62
93
63 """
94 """
64
95
65 def __init__(self, shell):
96 def __init__(self, shell):
66 super(ExecutionMagics, self).__init__(shell)
97 super(ExecutionMagics, self).__init__(shell)
67 if profile is None:
98 if profile is None:
68 self.prun = self.profile_missing_notice
99 self.prun = self.profile_missing_notice
69 # Default execution function used to actually run user code.
100 # Default execution function used to actually run user code.
70 self.default_runner = None
101 self.default_runner = None
71
102
72 def profile_missing_notice(self, *args, **kwargs):
103 def profile_missing_notice(self, *args, **kwargs):
73 error("""\
104 error("""\
74 The profile module could not be found. It has been removed from the standard
105 The profile module could not be found. It has been removed from the standard
75 python packages because of its non-free license. To use profiling, install the
106 python packages because of its non-free license. To use profiling, install the
76 python-profiler package from non-free.""")
107 python-profiler package from non-free.""")
77
108
78 @skip_doctest
109 @skip_doctest
79 @line_cell_magic
110 @line_cell_magic
80 def prun(self, parameter_s='', cell=None):
111 def prun(self, parameter_s='', cell=None):
81
112
82 """Run a statement through the python code profiler.
113 """Run a statement through the python code profiler.
83
114
84 Usage, in line mode:
115 Usage, in line mode:
85 %prun [options] statement
116 %prun [options] statement
86
117
87 Usage, in cell mode:
118 Usage, in cell mode:
88 %%prun [options] [statement]
119 %%prun [options] [statement]
89 code...
120 code...
90 code...
121 code...
91
122
92 In cell mode, the additional code lines are appended to the (possibly
123 In cell mode, the additional code lines are appended to the (possibly
93 empty) statement in the first line. Cell mode allows you to easily
124 empty) statement in the first line. Cell mode allows you to easily
94 profile multiline blocks without having to put them in a separate
125 profile multiline blocks without having to put them in a separate
95 function.
126 function.
96
127
97 The given statement (which doesn't require quote marks) is run via the
128 The given statement (which doesn't require quote marks) is run via the
98 python profiler in a manner similar to the profile.run() function.
129 python profiler in a manner similar to the profile.run() function.
99 Namespaces are internally managed to work correctly; profile.run
130 Namespaces are internally managed to work correctly; profile.run
100 cannot be used in IPython because it makes certain assumptions about
131 cannot be used in IPython because it makes certain assumptions about
101 namespaces which do not hold under IPython.
132 namespaces which do not hold under IPython.
102
133
103 Options:
134 Options:
104
135
105 -l <limit>
136 -l <limit>
106 you can place restrictions on what or how much of the
137 you can place restrictions on what or how much of the
107 profile gets printed. The limit value can be:
138 profile gets printed. The limit value can be:
108
139
109 * A string: only information for function names containing this string
140 * A string: only information for function names containing this string
110 is printed.
141 is printed.
111
142
112 * An integer: only these many lines are printed.
143 * An integer: only these many lines are printed.
113
144
114 * A float (between 0 and 1): this fraction of the report is printed
145 * A float (between 0 and 1): this fraction of the report is printed
115 (for example, use a limit of 0.4 to see the topmost 40% only).
146 (for example, use a limit of 0.4 to see the topmost 40% only).
116
147
117 You can combine several limits with repeated use of the option. For
148 You can combine several limits with repeated use of the option. For
118 example, ``-l __init__ -l 5`` will print only the topmost 5 lines of
149 example, ``-l __init__ -l 5`` will print only the topmost 5 lines of
119 information about class constructors.
150 information about class constructors.
120
151
121 -r
152 -r
122 return the pstats.Stats object generated by the profiling. This
153 return the pstats.Stats object generated by the profiling. This
123 object has all the information about the profile in it, and you can
154 object has all the information about the profile in it, and you can
124 later use it for further analysis or in other functions.
155 later use it for further analysis or in other functions.
125
156
126 -s <key>
157 -s <key>
127 sort profile by given key. You can provide more than one key
158 sort profile by given key. You can provide more than one key
128 by using the option several times: '-s key1 -s key2 -s key3...'. The
159 by using the option several times: '-s key1 -s key2 -s key3...'. The
129 default sorting key is 'time'.
160 default sorting key is 'time'.
130
161
131 The following is copied verbatim from the profile documentation
162 The following is copied verbatim from the profile documentation
132 referenced below:
163 referenced below:
133
164
134 When more than one key is provided, additional keys are used as
165 When more than one key is provided, additional keys are used as
135 secondary criteria when the there is equality in all keys selected
166 secondary criteria when the there is equality in all keys selected
136 before them.
167 before them.
137
168
138 Abbreviations can be used for any key names, as long as the
169 Abbreviations can be used for any key names, as long as the
139 abbreviation is unambiguous. The following are the keys currently
170 abbreviation is unambiguous. The following are the keys currently
140 defined:
171 defined:
141
172
142 ============ =====================
173 ============ =====================
143 Valid Arg Meaning
174 Valid Arg Meaning
144 ============ =====================
175 ============ =====================
145 "calls" call count
176 "calls" call count
146 "cumulative" cumulative time
177 "cumulative" cumulative time
147 "file" file name
178 "file" file name
148 "module" file name
179 "module" file name
149 "pcalls" primitive call count
180 "pcalls" primitive call count
150 "line" line number
181 "line" line number
151 "name" function name
182 "name" function name
152 "nfl" name/file/line
183 "nfl" name/file/line
153 "stdname" standard name
184 "stdname" standard name
154 "time" internal time
185 "time" internal time
155 ============ =====================
186 ============ =====================
156
187
157 Note that all sorts on statistics are in descending order (placing
188 Note that all sorts on statistics are in descending order (placing
158 most time consuming items first), where as name, file, and line number
189 most time consuming items first), where as name, file, and line number
159 searches are in ascending order (i.e., alphabetical). The subtle
190 searches are in ascending order (i.e., alphabetical). The subtle
160 distinction between "nfl" and "stdname" is that the standard name is a
191 distinction between "nfl" and "stdname" is that the standard name is a
161 sort of the name as printed, which means that the embedded line
192 sort of the name as printed, which means that the embedded line
162 numbers get compared in an odd way. For example, lines 3, 20, and 40
193 numbers get compared in an odd way. For example, lines 3, 20, and 40
163 would (if the file names were the same) appear in the string order
194 would (if the file names were the same) appear in the string order
164 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
195 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
165 line numbers. In fact, sort_stats("nfl") is the same as
196 line numbers. In fact, sort_stats("nfl") is the same as
166 sort_stats("name", "file", "line").
197 sort_stats("name", "file", "line").
167
198
168 -T <filename>
199 -T <filename>
169 save profile results as shown on screen to a text
200 save profile results as shown on screen to a text
170 file. The profile is still shown on screen.
201 file. The profile is still shown on screen.
171
202
172 -D <filename>
203 -D <filename>
173 save (via dump_stats) profile statistics to given
204 save (via dump_stats) profile statistics to given
174 filename. This data is in a format understood by the pstats module, and
205 filename. This data is in a format understood by the pstats module, and
175 is generated by a call to the dump_stats() method of profile
206 is generated by a call to the dump_stats() method of profile
176 objects. The profile is still shown on screen.
207 objects. The profile is still shown on screen.
177
208
178 -q
209 -q
179 suppress output to the pager. Best used with -T and/or -D above.
210 suppress output to the pager. Best used with -T and/or -D above.
180
211
181 If you want to run complete programs under the profiler's control, use
212 If you want to run complete programs under the profiler's control, use
182 ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts
213 ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts
183 contains profiler specific options as described here.
214 contains profiler specific options as described here.
184
215
185 You can read the complete documentation for the profile module with::
216 You can read the complete documentation for the profile module with::
186
217
187 In [1]: import profile; profile.help()
218 In [1]: import profile; profile.help()
188 """
219 """
189 opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q',
220 opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q',
190 list_all=True, posix=False)
221 list_all=True, posix=False)
191 if cell is not None:
222 if cell is not None:
192 arg_str += '\n' + cell
223 arg_str += '\n' + cell
193 arg_str = self.shell.input_splitter.transform_cell(arg_str)
224 arg_str = self.shell.input_splitter.transform_cell(arg_str)
194 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
225 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
195
226
196 def _run_with_profiler(self, code, opts, namespace):
227 def _run_with_profiler(self, code, opts, namespace):
197 """
228 """
198 Run `code` with profiler. Used by ``%prun`` and ``%run -p``.
229 Run `code` with profiler. Used by ``%prun`` and ``%run -p``.
199
230
200 Parameters
231 Parameters
201 ----------
232 ----------
202 code : str
233 code : str
203 Code to be executed.
234 Code to be executed.
204 opts : Struct
235 opts : Struct
205 Options parsed by `self.parse_options`.
236 Options parsed by `self.parse_options`.
206 namespace : dict
237 namespace : dict
207 A dictionary for Python namespace (e.g., `self.shell.user_ns`).
238 A dictionary for Python namespace (e.g., `self.shell.user_ns`).
208
239
209 """
240 """
210
241
211 # Fill default values for unspecified options:
242 # Fill default values for unspecified options:
212 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
243 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
213
244
214 prof = profile.Profile()
245 prof = profile.Profile()
215 try:
246 try:
216 prof = prof.runctx(code, namespace, namespace)
247 prof = prof.runctx(code, namespace, namespace)
217 sys_exit = ''
248 sys_exit = ''
218 except SystemExit:
249 except SystemExit:
219 sys_exit = """*** SystemExit exception caught in code being profiled."""
250 sys_exit = """*** SystemExit exception caught in code being profiled."""
220
251
221 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
252 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
222
253
223 lims = opts.l
254 lims = opts.l
224 if lims:
255 if lims:
225 lims = [] # rebuild lims with ints/floats/strings
256 lims = [] # rebuild lims with ints/floats/strings
226 for lim in opts.l:
257 for lim in opts.l:
227 try:
258 try:
228 lims.append(int(lim))
259 lims.append(int(lim))
229 except ValueError:
260 except ValueError:
230 try:
261 try:
231 lims.append(float(lim))
262 lims.append(float(lim))
232 except ValueError:
263 except ValueError:
233 lims.append(lim)
264 lims.append(lim)
234
265
235 # Trap output.
266 # Trap output.
236 stdout_trap = StringIO()
267 stdout_trap = StringIO()
237 stats_stream = stats.stream
268 stats_stream = stats.stream
238 try:
269 try:
239 stats.stream = stdout_trap
270 stats.stream = stdout_trap
240 stats.print_stats(*lims)
271 stats.print_stats(*lims)
241 finally:
272 finally:
242 stats.stream = stats_stream
273 stats.stream = stats_stream
243
274
244 output = stdout_trap.getvalue()
275 output = stdout_trap.getvalue()
245 output = output.rstrip()
276 output = output.rstrip()
246
277
247 if 'q' not in opts:
278 if 'q' not in opts:
248 page.page(output)
279 page.page(output)
249 print sys_exit,
280 print sys_exit,
250
281
251 dump_file = opts.D[0]
282 dump_file = opts.D[0]
252 text_file = opts.T[0]
283 text_file = opts.T[0]
253 if dump_file:
284 if dump_file:
254 dump_file = unquote_filename(dump_file)
285 dump_file = unquote_filename(dump_file)
255 prof.dump_stats(dump_file)
286 prof.dump_stats(dump_file)
256 print '\n*** Profile stats marshalled to file',\
287 print '\n*** Profile stats marshalled to file',\
257 repr(dump_file)+'.',sys_exit
288 repr(dump_file)+'.',sys_exit
258 if text_file:
289 if text_file:
259 text_file = unquote_filename(text_file)
290 text_file = unquote_filename(text_file)
260 pfile = open(text_file,'w')
291 pfile = open(text_file,'w')
261 pfile.write(output)
292 pfile.write(output)
262 pfile.close()
293 pfile.close()
263 print '\n*** Profile printout saved to text file',\
294 print '\n*** Profile printout saved to text file',\
264 repr(text_file)+'.',sys_exit
295 repr(text_file)+'.',sys_exit
265
296
266 if 'r' in opts:
297 if 'r' in opts:
267 return stats
298 return stats
268 else:
299 else:
269 return None
300 return None
270
301
271 @line_magic
302 @line_magic
272 def pdb(self, parameter_s=''):
303 def pdb(self, parameter_s=''):
273 """Control the automatic calling of the pdb interactive debugger.
304 """Control the automatic calling of the pdb interactive debugger.
274
305
275 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
306 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
276 argument it works as a toggle.
307 argument it works as a toggle.
277
308
278 When an exception is triggered, IPython can optionally call the
309 When an exception is triggered, IPython can optionally call the
279 interactive pdb debugger after the traceback printout. %pdb toggles
310 interactive pdb debugger after the traceback printout. %pdb toggles
280 this feature on and off.
311 this feature on and off.
281
312
282 The initial state of this feature is set in your configuration
313 The initial state of this feature is set in your configuration
283 file (the option is ``InteractiveShell.pdb``).
314 file (the option is ``InteractiveShell.pdb``).
284
315
285 If you want to just activate the debugger AFTER an exception has fired,
316 If you want to just activate the debugger AFTER an exception has fired,
286 without having to type '%pdb on' and rerunning your code, you can use
317 without having to type '%pdb on' and rerunning your code, you can use
287 the %debug magic."""
318 the %debug magic."""
288
319
289 par = parameter_s.strip().lower()
320 par = parameter_s.strip().lower()
290
321
291 if par:
322 if par:
292 try:
323 try:
293 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
324 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
294 except KeyError:
325 except KeyError:
295 print ('Incorrect argument. Use on/1, off/0, '
326 print ('Incorrect argument. Use on/1, off/0, '
296 'or nothing for a toggle.')
327 'or nothing for a toggle.')
297 return
328 return
298 else:
329 else:
299 # toggle
330 # toggle
300 new_pdb = not self.shell.call_pdb
331 new_pdb = not self.shell.call_pdb
301
332
302 # set on the shell
333 # set on the shell
303 self.shell.call_pdb = new_pdb
334 self.shell.call_pdb = new_pdb
304 print 'Automatic pdb calling has been turned',on_off(new_pdb)
335 print 'Automatic pdb calling has been turned',on_off(new_pdb)
305
336
306 @skip_doctest
337 @skip_doctest
307 @magic_arguments.magic_arguments()
338 @magic_arguments.magic_arguments()
308 @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE',
339 @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE',
309 help="""
340 help="""
310 Set break point at LINE in FILE.
341 Set break point at LINE in FILE.
311 """
342 """
312 )
343 )
313 @magic_arguments.argument('statement', nargs='*',
344 @magic_arguments.argument('statement', nargs='*',
314 help="""
345 help="""
315 Code to run in debugger.
346 Code to run in debugger.
316 You can omit this in cell magic mode.
347 You can omit this in cell magic mode.
317 """
348 """
318 )
349 )
319 @line_cell_magic
350 @line_cell_magic
320 def debug(self, line='', cell=None):
351 def debug(self, line='', cell=None):
321 """Activate the interactive debugger.
352 """Activate the interactive debugger.
322
353
323 This magic command support two ways of activating debugger.
354 This magic command support two ways of activating debugger.
324 One is to activate debugger before executing code. This way, you
355 One is to activate debugger before executing code. This way, you
325 can set a break point, to step through the code from the point.
356 can set a break point, to step through the code from the point.
326 You can use this mode by giving statements to execute and optionally
357 You can use this mode by giving statements to execute and optionally
327 a breakpoint.
358 a breakpoint.
328
359
329 The other one is to activate debugger in post-mortem mode. You can
360 The other one is to activate debugger in post-mortem mode. You can
330 activate this mode simply running %debug without any argument.
361 activate this mode simply running %debug without any argument.
331 If an exception has just occurred, this lets you inspect its stack
362 If an exception has just occurred, this lets you inspect its stack
332 frames interactively. Note that this will always work only on the last
363 frames interactively. Note that this will always work only on the last
333 traceback that occurred, so you must call this quickly after an
364 traceback that occurred, so you must call this quickly after an
334 exception that you wish to inspect has fired, because if another one
365 exception that you wish to inspect has fired, because if another one
335 occurs, it clobbers the previous one.
366 occurs, it clobbers the previous one.
336
367
337 If you want IPython to automatically do this on every exception, see
368 If you want IPython to automatically do this on every exception, see
338 the %pdb magic for more details.
369 the %pdb magic for more details.
339 """
370 """
340 args = magic_arguments.parse_argstring(self.debug, line)
371 args = magic_arguments.parse_argstring(self.debug, line)
341
372
342 if not (args.breakpoint or args.statement or cell):
373 if not (args.breakpoint or args.statement or cell):
343 self._debug_post_mortem()
374 self._debug_post_mortem()
344 else:
375 else:
345 code = "\n".join(args.statement)
376 code = "\n".join(args.statement)
346 if cell:
377 if cell:
347 code += "\n" + cell
378 code += "\n" + cell
348 self._debug_exec(code, args.breakpoint)
379 self._debug_exec(code, args.breakpoint)
349
380
350 def _debug_post_mortem(self):
381 def _debug_post_mortem(self):
351 self.shell.debugger(force=True)
382 self.shell.debugger(force=True)
352
383
353 def _debug_exec(self, code, breakpoint):
384 def _debug_exec(self, code, breakpoint):
354 if breakpoint:
385 if breakpoint:
355 (filename, bp_line) = breakpoint.split(':', 1)
386 (filename, bp_line) = breakpoint.split(':', 1)
356 bp_line = int(bp_line)
387 bp_line = int(bp_line)
357 else:
388 else:
358 (filename, bp_line) = (None, None)
389 (filename, bp_line) = (None, None)
359 self._run_with_debugger(code, self.shell.user_ns, filename, bp_line)
390 self._run_with_debugger(code, self.shell.user_ns, filename, bp_line)
360
391
361 @line_magic
392 @line_magic
362 def tb(self, s):
393 def tb(self, s):
363 """Print the last traceback with the currently active exception mode.
394 """Print the last traceback with the currently active exception mode.
364
395
365 See %xmode for changing exception reporting modes."""
396 See %xmode for changing exception reporting modes."""
366 self.shell.showtraceback()
397 self.shell.showtraceback()
367
398
368 @skip_doctest
399 @skip_doctest
369 @line_magic
400 @line_magic
370 def run(self, parameter_s='', runner=None,
401 def run(self, parameter_s='', runner=None,
371 file_finder=get_py_filename):
402 file_finder=get_py_filename):
372 """Run the named file inside IPython as a program.
403 """Run the named file inside IPython as a program.
373
404
374 Usage::
405 Usage::
375
406
376 %run [-n -i -e -G]
407 %run [-n -i -e -G]
377 [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
408 [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
378 ( -m mod | file ) [args]
409 ( -m mod | file ) [args]
379
410
380 Parameters after the filename are passed as command-line arguments to
411 Parameters after the filename are passed as command-line arguments to
381 the program (put in sys.argv). Then, control returns to IPython's
412 the program (put in sys.argv). Then, control returns to IPython's
382 prompt.
413 prompt.
383
414
384 This is similar to running at a system prompt ``python file args``,
415 This is similar to running at a system prompt ``python file args``,
385 but with the advantage of giving you IPython's tracebacks, and of
416 but with the advantage of giving you IPython's tracebacks, and of
386 loading all variables into your interactive namespace for further use
417 loading all variables into your interactive namespace for further use
387 (unless -p is used, see below).
418 (unless -p is used, see below).
388
419
389 The file is executed in a namespace initially consisting only of
420 The file is executed in a namespace initially consisting only of
390 ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus
421 ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus
391 sees its environment as if it were being run as a stand-alone program
422 sees its environment as if it were being run as a stand-alone program
392 (except for sharing global objects such as previously imported
423 (except for sharing global objects such as previously imported
393 modules). But after execution, the IPython interactive namespace gets
424 modules). But after execution, the IPython interactive namespace gets
394 updated with all variables defined in the program (except for __name__
425 updated with all variables defined in the program (except for __name__
395 and sys.argv). This allows for very convenient loading of code for
426 and sys.argv). This allows for very convenient loading of code for
396 interactive work, while giving each program a 'clean sheet' to run in.
427 interactive work, while giving each program a 'clean sheet' to run in.
397
428
398 Arguments are expanded using shell-like glob match. Patterns
429 Arguments are expanded using shell-like glob match. Patterns
399 '*', '?', '[seq]' and '[!seq]' can be used. Additionally,
430 '*', '?', '[seq]' and '[!seq]' can be used. Additionally,
400 tilde '~' will be expanded into user's home directory. Unlike
431 tilde '~' will be expanded into user's home directory. Unlike
401 real shells, quotation does not suppress expansions. Use
432 real shells, quotation does not suppress expansions. Use
402 *two* back slashes (e.g. ``\\\\*``) to suppress expansions.
433 *two* back slashes (e.g. ``\\\\*``) to suppress expansions.
403 To completely disable these expansions, you can use -G flag.
434 To completely disable these expansions, you can use -G flag.
404
435
405 Options:
436 Options:
406
437
407 -n
438 -n
408 __name__ is NOT set to '__main__', but to the running file's name
439 __name__ is NOT set to '__main__', but to the running file's name
409 without extension (as python does under import). This allows running
440 without extension (as python does under import). This allows running
410 scripts and reloading the definitions in them without calling code
441 scripts and reloading the definitions in them without calling code
411 protected by an ``if __name__ == "__main__"`` clause.
442 protected by an ``if __name__ == "__main__"`` clause.
412
443
413 -i
444 -i
414 run the file in IPython's namespace instead of an empty one. This
445 run the file in IPython's namespace instead of an empty one. This
415 is useful if you are experimenting with code written in a text editor
446 is useful if you are experimenting with code written in a text editor
416 which depends on variables defined interactively.
447 which depends on variables defined interactively.
417
448
418 -e
449 -e
419 ignore sys.exit() calls or SystemExit exceptions in the script
450 ignore sys.exit() calls or SystemExit exceptions in the script
420 being run. This is particularly useful if IPython is being used to
451 being run. This is particularly useful if IPython is being used to
421 run unittests, which always exit with a sys.exit() call. In such
452 run unittests, which always exit with a sys.exit() call. In such
422 cases you are interested in the output of the test results, not in
453 cases you are interested in the output of the test results, not in
423 seeing a traceback of the unittest module.
454 seeing a traceback of the unittest module.
424
455
425 -t
456 -t
426 print timing information at the end of the run. IPython will give
457 print timing information at the end of the run. IPython will give
427 you an estimated CPU time consumption for your script, which under
458 you an estimated CPU time consumption for your script, which under
428 Unix uses the resource module to avoid the wraparound problems of
459 Unix uses the resource module to avoid the wraparound problems of
429 time.clock(). Under Unix, an estimate of time spent on system tasks
460 time.clock(). Under Unix, an estimate of time spent on system tasks
430 is also given (for Windows platforms this is reported as 0.0).
461 is also given (for Windows platforms this is reported as 0.0).
431
462
432 If -t is given, an additional ``-N<N>`` option can be given, where <N>
463 If -t is given, an additional ``-N<N>`` option can be given, where <N>
433 must be an integer indicating how many times you want the script to
464 must be an integer indicating how many times you want the script to
434 run. The final timing report will include total and per run results.
465 run. The final timing report will include total and per run results.
435
466
436 For example (testing the script uniq_stable.py)::
467 For example (testing the script uniq_stable.py)::
437
468
438 In [1]: run -t uniq_stable
469 In [1]: run -t uniq_stable
439
470
440 IPython CPU timings (estimated):
471 IPython CPU timings (estimated):
441 User : 0.19597 s.
472 User : 0.19597 s.
442 System: 0.0 s.
473 System: 0.0 s.
443
474
444 In [2]: run -t -N5 uniq_stable
475 In [2]: run -t -N5 uniq_stable
445
476
446 IPython CPU timings (estimated):
477 IPython CPU timings (estimated):
447 Total runs performed: 5
478 Total runs performed: 5
448 Times : Total Per run
479 Times : Total Per run
449 User : 0.910862 s, 0.1821724 s.
480 User : 0.910862 s, 0.1821724 s.
450 System: 0.0 s, 0.0 s.
481 System: 0.0 s, 0.0 s.
451
482
452 -d
483 -d
453 run your program under the control of pdb, the Python debugger.
484 run your program under the control of pdb, the Python debugger.
454 This allows you to execute your program step by step, watch variables,
485 This allows you to execute your program step by step, watch variables,
455 etc. Internally, what IPython does is similar to calling::
486 etc. Internally, what IPython does is similar to calling::
456
487
457 pdb.run('execfile("YOURFILENAME")')
488 pdb.run('execfile("YOURFILENAME")')
458
489
459 with a breakpoint set on line 1 of your file. You can change the line
490 with a breakpoint set on line 1 of your file. You can change the line
460 number for this automatic breakpoint to be <N> by using the -bN option
491 number for this automatic breakpoint to be <N> by using the -bN option
461 (where N must be an integer). For example::
492 (where N must be an integer). For example::
462
493
463 %run -d -b40 myscript
494 %run -d -b40 myscript
464
495
465 will set the first breakpoint at line 40 in myscript.py. Note that
496 will set the first breakpoint at line 40 in myscript.py. Note that
466 the first breakpoint must be set on a line which actually does
497 the first breakpoint must be set on a line which actually does
467 something (not a comment or docstring) for it to stop execution.
498 something (not a comment or docstring) for it to stop execution.
468
499
469 Or you can specify a breakpoint in a different file::
500 Or you can specify a breakpoint in a different file::
470
501
471 %run -d -b myotherfile.py:20 myscript
502 %run -d -b myotherfile.py:20 myscript
472
503
473 When the pdb debugger starts, you will see a (Pdb) prompt. You must
504 When the pdb debugger starts, you will see a (Pdb) prompt. You must
474 first enter 'c' (without quotes) to start execution up to the first
505 first enter 'c' (without quotes) to start execution up to the first
475 breakpoint.
506 breakpoint.
476
507
477 Entering 'help' gives information about the use of the debugger. You
508 Entering 'help' gives information about the use of the debugger. You
478 can easily see pdb's full documentation with "import pdb;pdb.help()"
509 can easily see pdb's full documentation with "import pdb;pdb.help()"
479 at a prompt.
510 at a prompt.
480
511
481 -p
512 -p
482 run program under the control of the Python profiler module (which
513 run program under the control of the Python profiler module (which
483 prints a detailed report of execution times, function calls, etc).
514 prints a detailed report of execution times, function calls, etc).
484
515
485 You can pass other options after -p which affect the behavior of the
516 You can pass other options after -p which affect the behavior of the
486 profiler itself. See the docs for %prun for details.
517 profiler itself. See the docs for %prun for details.
487
518
488 In this mode, the program's variables do NOT propagate back to the
519 In this mode, the program's variables do NOT propagate back to the
489 IPython interactive namespace (because they remain in the namespace
520 IPython interactive namespace (because they remain in the namespace
490 where the profiler executes them).
521 where the profiler executes them).
491
522
492 Internally this triggers a call to %prun, see its documentation for
523 Internally this triggers a call to %prun, see its documentation for
493 details on the options available specifically for profiling.
524 details on the options available specifically for profiling.
494
525
495 There is one special usage for which the text above doesn't apply:
526 There is one special usage for which the text above doesn't apply:
496 if the filename ends with .ipy, the file is run as ipython script,
527 if the filename ends with .ipy, the file is run as ipython script,
497 just as if the commands were written on IPython prompt.
528 just as if the commands were written on IPython prompt.
498
529
499 -m
530 -m
500 specify module name to load instead of script path. Similar to
531 specify module name to load instead of script path. Similar to
501 the -m option for the python interpreter. Use this option last if you
532 the -m option for the python interpreter. Use this option last if you
502 want to combine with other %run options. Unlike the python interpreter
533 want to combine with other %run options. Unlike the python interpreter
503 only source modules are allowed no .pyc or .pyo files.
534 only source modules are allowed no .pyc or .pyo files.
504 For example::
535 For example::
505
536
506 %run -m example
537 %run -m example
507
538
508 will run the example module.
539 will run the example module.
509
540
510 -G
541 -G
511 disable shell-like glob expansion of arguments.
542 disable shell-like glob expansion of arguments.
512
543
513 """
544 """
514
545
515 # get arguments and set sys.argv for program to be run.
546 # get arguments and set sys.argv for program to be run.
516 opts, arg_lst = self.parse_options(parameter_s,
547 opts, arg_lst = self.parse_options(parameter_s,
517 'nidtN:b:pD:l:rs:T:em:G',
548 'nidtN:b:pD:l:rs:T:em:G',
518 mode='list', list_all=1)
549 mode='list', list_all=1)
519 if "m" in opts:
550 if "m" in opts:
520 modulename = opts["m"][0]
551 modulename = opts["m"][0]
521 modpath = find_mod(modulename)
552 modpath = find_mod(modulename)
522 if modpath is None:
553 if modpath is None:
523 warn('%r is not a valid modulename on sys.path'%modulename)
554 warn('%r is not a valid modulename on sys.path'%modulename)
524 return
555 return
525 arg_lst = [modpath] + arg_lst
556 arg_lst = [modpath] + arg_lst
526 try:
557 try:
527 filename = file_finder(arg_lst[0])
558 filename = file_finder(arg_lst[0])
528 except IndexError:
559 except IndexError:
529 warn('you must provide at least a filename.')
560 warn('you must provide at least a filename.')
530 print '\n%run:\n', oinspect.getdoc(self.run)
561 print '\n%run:\n', oinspect.getdoc(self.run)
531 return
562 return
532 except IOError as e:
563 except IOError as e:
533 try:
564 try:
534 msg = str(e)
565 msg = str(e)
535 except UnicodeError:
566 except UnicodeError:
536 msg = e.message
567 msg = e.message
537 error(msg)
568 error(msg)
538 return
569 return
539
570
540 if filename.lower().endswith('.ipy'):
571 if filename.lower().endswith('.ipy'):
541 with preserve_keys(self.shell.user_ns, '__file__'):
572 with preserve_keys(self.shell.user_ns, '__file__'):
542 self.shell.user_ns['__file__'] = filename
573 self.shell.user_ns['__file__'] = filename
543 self.shell.safe_execfile_ipy(filename)
574 self.shell.safe_execfile_ipy(filename)
544 return
575 return
545
576
546 # Control the response to exit() calls made by the script being run
577 # Control the response to exit() calls made by the script being run
547 exit_ignore = 'e' in opts
578 exit_ignore = 'e' in opts
548
579
549 # Make sure that the running script gets a proper sys.argv as if it
580 # Make sure that the running script gets a proper sys.argv as if it
550 # were run from a system shell.
581 # were run from a system shell.
551 save_argv = sys.argv # save it for later restoring
582 save_argv = sys.argv # save it for later restoring
552
583
553 if 'G' in opts:
584 if 'G' in opts:
554 args = arg_lst[1:]
585 args = arg_lst[1:]
555 else:
586 else:
556 # tilde and glob expansion
587 # tilde and glob expansion
557 args = shellglob(map(os.path.expanduser, arg_lst[1:]))
588 args = shellglob(map(os.path.expanduser, arg_lst[1:]))
558
589
559 sys.argv = [filename] + args # put in the proper filename
590 sys.argv = [filename] + args # put in the proper filename
560 # protect sys.argv from potential unicode strings on Python 2:
591 # protect sys.argv from potential unicode strings on Python 2:
561 if not py3compat.PY3:
592 if not py3compat.PY3:
562 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
593 sys.argv = [ py3compat.cast_bytes(a) for a in sys.argv ]
563
594
564 if 'i' in opts:
595 if 'i' in opts:
565 # Run in user's interactive namespace
596 # Run in user's interactive namespace
566 prog_ns = self.shell.user_ns
597 prog_ns = self.shell.user_ns
567 __name__save = self.shell.user_ns['__name__']
598 __name__save = self.shell.user_ns['__name__']
568 prog_ns['__name__'] = '__main__'
599 prog_ns['__name__'] = '__main__'
569 main_mod = self.shell.user_module
600 main_mod = self.shell.user_module
570
601
571 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
602 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
572 # set the __file__ global in the script's namespace
603 # set the __file__ global in the script's namespace
573 # TK: Is this necessary in interactive mode?
604 # TK: Is this necessary in interactive mode?
574 prog_ns['__file__'] = filename
605 prog_ns['__file__'] = filename
575 else:
606 else:
576 # Run in a fresh, empty namespace
607 # Run in a fresh, empty namespace
577 if 'n' in opts:
608 if 'n' in opts:
578 name = os.path.splitext(os.path.basename(filename))[0]
609 name = os.path.splitext(os.path.basename(filename))[0]
579 else:
610 else:
580 name = '__main__'
611 name = '__main__'
581
612
582 # The shell MUST hold a reference to prog_ns so after %run
613 # The shell MUST hold a reference to prog_ns so after %run
583 # exits, the python deletion mechanism doesn't zero it out
614 # exits, the python deletion mechanism doesn't zero it out
584 # (leaving dangling references). See interactiveshell for details
615 # (leaving dangling references). See interactiveshell for details
585 main_mod = self.shell.new_main_mod(filename, name)
616 main_mod = self.shell.new_main_mod(filename, name)
586 prog_ns = main_mod.__dict__
617 prog_ns = main_mod.__dict__
587
618
588 # pickle fix. See interactiveshell for an explanation. But we need to
619 # pickle fix. See interactiveshell for an explanation. But we need to
589 # make sure that, if we overwrite __main__, we replace it at the end
620 # make sure that, if we overwrite __main__, we replace it at the end
590 main_mod_name = prog_ns['__name__']
621 main_mod_name = prog_ns['__name__']
591
622
592 if main_mod_name == '__main__':
623 if main_mod_name == '__main__':
593 restore_main = sys.modules['__main__']
624 restore_main = sys.modules['__main__']
594 else:
625 else:
595 restore_main = False
626 restore_main = False
596
627
597 # This needs to be undone at the end to prevent holding references to
628 # This needs to be undone at the end to prevent holding references to
598 # every single object ever created.
629 # every single object ever created.
599 sys.modules[main_mod_name] = main_mod
630 sys.modules[main_mod_name] = main_mod
600
631
601 if 'p' in opts or 'd' in opts:
632 if 'p' in opts or 'd' in opts:
602 if 'm' in opts:
633 if 'm' in opts:
603 code = 'run_module(modulename, prog_ns)'
634 code = 'run_module(modulename, prog_ns)'
604 code_ns = {
635 code_ns = {
605 'run_module': self.shell.safe_run_module,
636 'run_module': self.shell.safe_run_module,
606 'prog_ns': prog_ns,
637 'prog_ns': prog_ns,
607 'modulename': modulename,
638 'modulename': modulename,
608 }
639 }
609 else:
640 else:
610 code = 'execfile(filename, prog_ns)'
641 code = 'execfile(filename, prog_ns)'
611 code_ns = {
642 code_ns = {
612 'execfile': self.shell.safe_execfile,
643 'execfile': self.shell.safe_execfile,
613 'prog_ns': prog_ns,
644 'prog_ns': prog_ns,
614 'filename': get_py_filename(filename),
645 'filename': get_py_filename(filename),
615 }
646 }
616
647
617 try:
648 try:
618 stats = None
649 stats = None
619 with self.shell.readline_no_record:
650 with self.shell.readline_no_record:
620 if 'p' in opts:
651 if 'p' in opts:
621 stats = self._run_with_profiler(code, opts, code_ns)
652 stats = self._run_with_profiler(code, opts, code_ns)
622 else:
653 else:
623 if 'd' in opts:
654 if 'd' in opts:
624 bp_file, bp_line = parse_breakpoint(
655 bp_file, bp_line = parse_breakpoint(
625 opts.get('b', ['1'])[0], filename)
656 opts.get('b', ['1'])[0], filename)
626 self._run_with_debugger(
657 self._run_with_debugger(
627 code, code_ns, filename, bp_line, bp_file)
658 code, code_ns, filename, bp_line, bp_file)
628 else:
659 else:
629 if 'm' in opts:
660 if 'm' in opts:
630 def run():
661 def run():
631 self.shell.safe_run_module(modulename, prog_ns)
662 self.shell.safe_run_module(modulename, prog_ns)
632 else:
663 else:
633 if runner is None:
664 if runner is None:
634 runner = self.default_runner
665 runner = self.default_runner
635 if runner is None:
666 if runner is None:
636 runner = self.shell.safe_execfile
667 runner = self.shell.safe_execfile
637
668
638 def run():
669 def run():
639 runner(filename, prog_ns, prog_ns,
670 runner(filename, prog_ns, prog_ns,
640 exit_ignore=exit_ignore)
671 exit_ignore=exit_ignore)
641
672
642 if 't' in opts:
673 if 't' in opts:
643 # timed execution
674 # timed execution
644 try:
675 try:
645 nruns = int(opts['N'][0])
676 nruns = int(opts['N'][0])
646 if nruns < 1:
677 if nruns < 1:
647 error('Number of runs must be >=1')
678 error('Number of runs must be >=1')
648 return
679 return
649 except (KeyError):
680 except (KeyError):
650 nruns = 1
681 nruns = 1
651 self._run_with_timing(run, nruns)
682 self._run_with_timing(run, nruns)
652 else:
683 else:
653 # regular execution
684 # regular execution
654 run()
685 run()
655
686
656 if 'i' in opts:
687 if 'i' in opts:
657 self.shell.user_ns['__name__'] = __name__save
688 self.shell.user_ns['__name__'] = __name__save
658 else:
689 else:
659 # update IPython interactive namespace
690 # update IPython interactive namespace
660
691
661 # Some forms of read errors on the file may mean the
692 # Some forms of read errors on the file may mean the
662 # __name__ key was never set; using pop we don't have to
693 # __name__ key was never set; using pop we don't have to
663 # worry about a possible KeyError.
694 # worry about a possible KeyError.
664 prog_ns.pop('__name__', None)
695 prog_ns.pop('__name__', None)
665
696
666 with preserve_keys(self.shell.user_ns, '__file__'):
697 with preserve_keys(self.shell.user_ns, '__file__'):
667 self.shell.user_ns.update(prog_ns)
698 self.shell.user_ns.update(prog_ns)
668 finally:
699 finally:
669 # It's a bit of a mystery why, but __builtins__ can change from
700 # It's a bit of a mystery why, but __builtins__ can change from
670 # being a module to becoming a dict missing some key data after
701 # being a module to becoming a dict missing some key data after
671 # %run. As best I can see, this is NOT something IPython is doing
702 # %run. As best I can see, this is NOT something IPython is doing
672 # at all, and similar problems have been reported before:
703 # at all, and similar problems have been reported before:
673 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
704 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
674 # Since this seems to be done by the interpreter itself, the best
705 # Since this seems to be done by the interpreter itself, the best
675 # we can do is to at least restore __builtins__ for the user on
706 # we can do is to at least restore __builtins__ for the user on
676 # exit.
707 # exit.
677 self.shell.user_ns['__builtins__'] = builtin_mod
708 self.shell.user_ns['__builtins__'] = builtin_mod
678
709
679 # Ensure key global structures are restored
710 # Ensure key global structures are restored
680 sys.argv = save_argv
711 sys.argv = save_argv
681 if restore_main:
712 if restore_main:
682 sys.modules['__main__'] = restore_main
713 sys.modules['__main__'] = restore_main
683 else:
714 else:
684 # Remove from sys.modules the reference to main_mod we'd
715 # Remove from sys.modules the reference to main_mod we'd
685 # added. Otherwise it will trap references to objects
716 # added. Otherwise it will trap references to objects
686 # contained therein.
717 # contained therein.
687 del sys.modules[main_mod_name]
718 del sys.modules[main_mod_name]
688
719
689 return stats
720 return stats
690
721
691 def _run_with_debugger(self, code, code_ns, filename=None,
722 def _run_with_debugger(self, code, code_ns, filename=None,
692 bp_line=None, bp_file=None):
723 bp_line=None, bp_file=None):
693 """
724 """
694 Run `code` in debugger with a break point.
725 Run `code` in debugger with a break point.
695
726
696 Parameters
727 Parameters
697 ----------
728 ----------
698 code : str
729 code : str
699 Code to execute.
730 Code to execute.
700 code_ns : dict
731 code_ns : dict
701 A namespace in which `code` is executed.
732 A namespace in which `code` is executed.
702 filename : str
733 filename : str
703 `code` is ran as if it is in `filename`.
734 `code` is ran as if it is in `filename`.
704 bp_line : int, optional
735 bp_line : int, optional
705 Line number of the break point.
736 Line number of the break point.
706 bp_file : str, optional
737 bp_file : str, optional
707 Path to the file in which break point is specified.
738 Path to the file in which break point is specified.
708 `filename` is used if not given.
739 `filename` is used if not given.
709
740
710 Raises
741 Raises
711 ------
742 ------
712 UsageError
743 UsageError
713 If the break point given by `bp_line` is not valid.
744 If the break point given by `bp_line` is not valid.
714
745
715 """
746 """
716 deb = debugger.Pdb(self.shell.colors)
747 deb = debugger.Pdb(self.shell.colors)
717 # reset Breakpoint state, which is moronically kept
748 # reset Breakpoint state, which is moronically kept
718 # in a class
749 # in a class
719 bdb.Breakpoint.next = 1
750 bdb.Breakpoint.next = 1
720 bdb.Breakpoint.bplist = {}
751 bdb.Breakpoint.bplist = {}
721 bdb.Breakpoint.bpbynumber = [None]
752 bdb.Breakpoint.bpbynumber = [None]
722 if bp_line is not None:
753 if bp_line is not None:
723 # Set an initial breakpoint to stop execution
754 # Set an initial breakpoint to stop execution
724 maxtries = 10
755 maxtries = 10
725 bp_file = bp_file or filename
756 bp_file = bp_file or filename
726 checkline = deb.checkline(bp_file, bp_line)
757 checkline = deb.checkline(bp_file, bp_line)
727 if not checkline:
758 if not checkline:
728 for bp in range(bp_line + 1, bp_line + maxtries + 1):
759 for bp in range(bp_line + 1, bp_line + maxtries + 1):
729 if deb.checkline(bp_file, bp):
760 if deb.checkline(bp_file, bp):
730 break
761 break
731 else:
762 else:
732 msg = ("\nI failed to find a valid line to set "
763 msg = ("\nI failed to find a valid line to set "
733 "a breakpoint\n"
764 "a breakpoint\n"
734 "after trying up to line: %s.\n"
765 "after trying up to line: %s.\n"
735 "Please set a valid breakpoint manually "
766 "Please set a valid breakpoint manually "
736 "with the -b option." % bp)
767 "with the -b option." % bp)
737 raise UsageError(msg)
768 raise UsageError(msg)
738 # if we find a good linenumber, set the breakpoint
769 # if we find a good linenumber, set the breakpoint
739 deb.do_break('%s:%s' % (bp_file, bp_line))
770 deb.do_break('%s:%s' % (bp_file, bp_line))
740
771
741 if filename:
772 if filename:
742 # Mimic Pdb._runscript(...)
773 # Mimic Pdb._runscript(...)
743 deb._wait_for_mainpyfile = True
774 deb._wait_for_mainpyfile = True
744 deb.mainpyfile = deb.canonic(filename)
775 deb.mainpyfile = deb.canonic(filename)
745
776
746 # Start file run
777 # Start file run
747 print "NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt
778 print "NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt
748 try:
779 try:
749 if filename:
780 if filename:
750 # save filename so it can be used by methods on the deb object
781 # save filename so it can be used by methods on the deb object
751 deb._exec_filename = filename
782 deb._exec_filename = filename
752 deb.run(code, code_ns)
783 deb.run(code, code_ns)
753
784
754 except:
785 except:
755 etype, value, tb = sys.exc_info()
786 etype, value, tb = sys.exc_info()
756 # Skip three frames in the traceback: the %run one,
787 # Skip three frames in the traceback: the %run one,
757 # one inside bdb.py, and the command-line typed by the
788 # one inside bdb.py, and the command-line typed by the
758 # user (run by exec in pdb itself).
789 # user (run by exec in pdb itself).
759 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
790 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
760
791
761 @staticmethod
792 @staticmethod
762 def _run_with_timing(run, nruns):
793 def _run_with_timing(run, nruns):
763 """
794 """
764 Run function `run` and print timing information.
795 Run function `run` and print timing information.
765
796
766 Parameters
797 Parameters
767 ----------
798 ----------
768 run : callable
799 run : callable
769 Any callable object which takes no argument.
800 Any callable object which takes no argument.
770 nruns : int
801 nruns : int
771 Number of times to execute `run`.
802 Number of times to execute `run`.
772
803
773 """
804 """
774 twall0 = time.time()
805 twall0 = time.time()
775 if nruns == 1:
806 if nruns == 1:
776 t0 = clock2()
807 t0 = clock2()
777 run()
808 run()
778 t1 = clock2()
809 t1 = clock2()
779 t_usr = t1[0] - t0[0]
810 t_usr = t1[0] - t0[0]
780 t_sys = t1[1] - t0[1]
811 t_sys = t1[1] - t0[1]
781 print "\nIPython CPU timings (estimated):"
812 print "\nIPython CPU timings (estimated):"
782 print " User : %10.2f s." % t_usr
813 print " User : %10.2f s." % t_usr
783 print " System : %10.2f s." % t_sys
814 print " System : %10.2f s." % t_sys
784 else:
815 else:
785 runs = range(nruns)
816 runs = range(nruns)
786 t0 = clock2()
817 t0 = clock2()
787 for nr in runs:
818 for nr in runs:
788 run()
819 run()
789 t1 = clock2()
820 t1 = clock2()
790 t_usr = t1[0] - t0[0]
821 t_usr = t1[0] - t0[0]
791 t_sys = t1[1] - t0[1]
822 t_sys = t1[1] - t0[1]
792 print "\nIPython CPU timings (estimated):"
823 print "\nIPython CPU timings (estimated):"
793 print "Total runs performed:", nruns
824 print "Total runs performed:", nruns
794 print " Times : %10s %10s" % ('Total', 'Per run')
825 print " Times : %10s %10s" % ('Total', 'Per run')
795 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
826 print " User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns)
796 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
827 print " System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns)
797 twall1 = time.time()
828 twall1 = time.time()
798 print "Wall time: %10.2f s." % (twall1 - twall0)
829 print "Wall time: %10.2f s." % (twall1 - twall0)
799
830
800 @skip_doctest
831 @skip_doctest
801 @line_cell_magic
832 @line_cell_magic
802 def timeit(self, line='', cell=None):
833 def timeit(self, line='', cell=None):
803 """Time execution of a Python statement or expression
834 """Time execution of a Python statement or expression
804
835
805 Usage, in line mode:
836 Usage, in line mode:
806 %timeit [-n<N> -r<R> [-t|-c]] statement
837 %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
807 or in cell mode:
838 or in cell mode:
808 %%timeit [-n<N> -r<R> [-t|-c]] setup_code
839 %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
809 code
840 code
810 code...
841 code...
811
842
812 Time execution of a Python statement or expression using the timeit
843 Time execution of a Python statement or expression using the timeit
813 module. This function can be used both as a line and cell magic:
844 module. This function can be used both as a line and cell magic:
814
845
815 - In line mode you can time a single-line statement (though multiple
846 - In line mode you can time a single-line statement (though multiple
816 ones can be chained with using semicolons).
847 ones can be chained with using semicolons).
817
848
818 - In cell mode, the statement in the first line is used as setup code
849 - In cell mode, the statement in the first line is used as setup code
819 (executed but not timed) and the body of the cell is timed. The cell
850 (executed but not timed) and the body of the cell is timed. The cell
820 body has access to any variables created in the setup code.
851 body has access to any variables created in the setup code.
821
852
822 Options:
853 Options:
823 -n<N>: execute the given statement <N> times in a loop. If this value
854 -n<N>: execute the given statement <N> times in a loop. If this value
824 is not given, a fitting value is chosen.
855 is not given, a fitting value is chosen.
825
856
826 -r<R>: repeat the loop iteration <R> times and take the best result.
857 -r<R>: repeat the loop iteration <R> times and take the best result.
827 Default: 3
858 Default: 3
828
859
829 -t: use time.time to measure the time, which is the default on Unix.
860 -t: use time.time to measure the time, which is the default on Unix.
830 This function measures wall time.
861 This function measures wall time.
831
862
832 -c: use time.clock to measure the time, which is the default on
863 -c: use time.clock to measure the time, which is the default on
833 Windows and measures wall time. On Unix, resource.getrusage is used
864 Windows and measures wall time. On Unix, resource.getrusage is used
834 instead and returns the CPU user time.
865 instead and returns the CPU user time.
835
866
836 -p<P>: use a precision of <P> digits to display the timing result.
867 -p<P>: use a precision of <P> digits to display the timing result.
837 Default: 3
868 Default: 3
838
869
870 -q: Quiet, do not print result.
871
872 -o: return a TimeitResult that can be stored in a variable to inspect
873 the result in more details.
874
839
875
840 Examples
876 Examples
841 --------
877 --------
842 ::
878 ::
843
879
844 In [1]: %timeit pass
880 In [1]: %timeit pass
845 10000000 loops, best of 3: 53.3 ns per loop
881 10000000 loops, best of 3: 53.3 ns per loop
846
882
847 In [2]: u = None
883 In [2]: u = None
848
884
849 In [3]: %timeit u is None
885 In [3]: %timeit u is None
850 10000000 loops, best of 3: 184 ns per loop
886 10000000 loops, best of 3: 184 ns per loop
851
887
852 In [4]: %timeit -r 4 u == None
888 In [4]: %timeit -r 4 u == None
853 1000000 loops, best of 4: 242 ns per loop
889 1000000 loops, best of 4: 242 ns per loop
854
890
855 In [5]: import time
891 In [5]: import time
856
892
857 In [6]: %timeit -n1 time.sleep(2)
893 In [6]: %timeit -n1 time.sleep(2)
858 1 loops, best of 3: 2 s per loop
894 1 loops, best of 3: 2 s per loop
859
895
860
896
861 The times reported by %timeit will be slightly higher than those
897 The times reported by %timeit will be slightly higher than those
862 reported by the timeit.py script when variables are accessed. This is
898 reported by the timeit.py script when variables are accessed. This is
863 due to the fact that %timeit executes the statement in the namespace
899 due to the fact that %timeit executes the statement in the namespace
864 of the shell, compared with timeit.py, which uses a single setup
900 of the shell, compared with timeit.py, which uses a single setup
865 statement to import function or create variables. Generally, the bias
901 statement to import function or create variables. Generally, the bias
866 does not matter as long as results from timeit.py are not mixed with
902 does not matter as long as results from timeit.py are not mixed with
867 those from %timeit."""
903 those from %timeit."""
868
904
869 import timeit
905 import timeit
870
906
871 opts, stmt = self.parse_options(line,'n:r:tcp:',
907 opts, stmt = self.parse_options(line,'n:r:tcp:qo',
872 posix=False, strict=False)
908 posix=False, strict=False)
873 if stmt == "" and cell is None:
909 if stmt == "" and cell is None:
874 return
910 return
875
911
876 timefunc = timeit.default_timer
912 timefunc = timeit.default_timer
877 number = int(getattr(opts, "n", 0))
913 number = int(getattr(opts, "n", 0))
878 repeat = int(getattr(opts, "r", timeit.default_repeat))
914 repeat = int(getattr(opts, "r", timeit.default_repeat))
879 precision = int(getattr(opts, "p", 3))
915 precision = int(getattr(opts, "p", 3))
916 quiet = 'q' in opts
917 return_result = 'o' in opts
880 if hasattr(opts, "t"):
918 if hasattr(opts, "t"):
881 timefunc = time.time
919 timefunc = time.time
882 if hasattr(opts, "c"):
920 if hasattr(opts, "c"):
883 timefunc = clock
921 timefunc = clock
884
922
885 timer = timeit.Timer(timer=timefunc)
923 timer = timeit.Timer(timer=timefunc)
886 # this code has tight coupling to the inner workings of timeit.Timer,
924 # this code has tight coupling to the inner workings of timeit.Timer,
887 # but is there a better way to achieve that the code stmt has access
925 # but is there a better way to achieve that the code stmt has access
888 # to the shell namespace?
926 # to the shell namespace?
889 transform = self.shell.input_splitter.transform_cell
927 transform = self.shell.input_splitter.transform_cell
890
928
891 if cell is None:
929 if cell is None:
892 # called as line magic
930 # called as line magic
893 ast_setup = ast.parse("pass")
931 ast_setup = ast.parse("pass")
894 ast_stmt = ast.parse(transform(stmt))
932 ast_stmt = ast.parse(transform(stmt))
895 else:
933 else:
896 ast_setup = ast.parse(transform(stmt))
934 ast_setup = ast.parse(transform(stmt))
897 ast_stmt = ast.parse(transform(cell))
935 ast_stmt = ast.parse(transform(cell))
898
936
899 ast_setup = self.shell.transform_ast(ast_setup)
937 ast_setup = self.shell.transform_ast(ast_setup)
900 ast_stmt = self.shell.transform_ast(ast_stmt)
938 ast_stmt = self.shell.transform_ast(ast_stmt)
901
939
902 # This codestring is taken from timeit.template - we fill it in as an
940 # This codestring is taken from timeit.template - we fill it in as an
903 # AST, so that we can apply our AST transformations to the user code
941 # AST, so that we can apply our AST transformations to the user code
904 # without affecting the timing code.
942 # without affecting the timing code.
905 timeit_ast_template = ast.parse('def inner(_it, _timer):\n'
943 timeit_ast_template = ast.parse('def inner(_it, _timer):\n'
906 ' setup\n'
944 ' setup\n'
907 ' _t0 = _timer()\n'
945 ' _t0 = _timer()\n'
908 ' for _i in _it:\n'
946 ' for _i in _it:\n'
909 ' stmt\n'
947 ' stmt\n'
910 ' _t1 = _timer()\n'
948 ' _t1 = _timer()\n'
911 ' return _t1 - _t0\n')
949 ' return _t1 - _t0\n')
912
950
913 class TimeitTemplateFiller(ast.NodeTransformer):
951 class TimeitTemplateFiller(ast.NodeTransformer):
914 "This is quite tightly tied to the template definition above."
952 "This is quite tightly tied to the template definition above."
915 def visit_FunctionDef(self, node):
953 def visit_FunctionDef(self, node):
916 "Fill in the setup statement"
954 "Fill in the setup statement"
917 self.generic_visit(node)
955 self.generic_visit(node)
918 if node.name == "inner":
956 if node.name == "inner":
919 node.body[:1] = ast_setup.body
957 node.body[:1] = ast_setup.body
920
958
921 return node
959 return node
922
960
923 def visit_For(self, node):
961 def visit_For(self, node):
924 "Fill in the statement to be timed"
962 "Fill in the statement to be timed"
925 if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt':
963 if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt':
926 node.body = ast_stmt.body
964 node.body = ast_stmt.body
927 return node
965 return node
928
966
929 timeit_ast = TimeitTemplateFiller().visit(timeit_ast_template)
967 timeit_ast = TimeitTemplateFiller().visit(timeit_ast_template)
930 timeit_ast = ast.fix_missing_locations(timeit_ast)
968 timeit_ast = ast.fix_missing_locations(timeit_ast)
931
969
932 # Track compilation time so it can be reported if too long
970 # Track compilation time so it can be reported if too long
933 # Minimum time above which compilation time will be reported
971 # Minimum time above which compilation time will be reported
934 tc_min = 0.1
972 tc_min = 0.1
935
973
936 t0 = clock()
974 t0 = clock()
937 code = compile(timeit_ast, "<magic-timeit>", "exec")
975 code = compile(timeit_ast, "<magic-timeit>", "exec")
938 tc = clock()-t0
976 tc = clock()-t0
939
977
940 ns = {}
978 ns = {}
941 exec code in self.shell.user_ns, ns
979 exec code in self.shell.user_ns, ns
942 timer.inner = ns["inner"]
980 timer.inner = ns["inner"]
943
981
944 if number == 0:
982 if number == 0:
945 # determine number so that 0.2 <= total time < 2.0
983 # determine number so that 0.2 <= total time < 2.0
946 number = 1
984 number = 1
947 for i in range(1, 10):
985 for i in range(1, 10):
948 if timer.timeit(number) >= 0.2:
986 if timer.timeit(number) >= 0.2:
949 break
987 break
950 number *= 10
988 number *= 10
951
989 all_runs = timer.repeat(repeat, number)
952 best = min(timer.repeat(repeat, number)) / number
990 best = min(all_runs) / number
953
991 if not quiet :
954 print u"%d loops, best of %d: %s per loop" % (number, repeat,
992 print u"%d loops, best of %d: %s per loop" % (number, repeat,
955 _format_time(best, precision))
993 _format_time(best, precision))
956 if tc > tc_min:
994 if tc > tc_min:
957 print "Compiler time: %.2f s" % tc
995 print "Compiler time: %.2f s" % tc
996 if return_result:
997 return TimeitResult(number, repeat, best, all_runs, tc, precision)
958
998
959 @skip_doctest
999 @skip_doctest
960 @needs_local_scope
1000 @needs_local_scope
961 @line_cell_magic
1001 @line_cell_magic
962 def time(self,line='', cell=None, local_ns=None):
1002 def time(self,line='', cell=None, local_ns=None):
963 """Time execution of a Python statement or expression.
1003 """Time execution of a Python statement or expression.
964
1004
965 The CPU and wall clock times are printed, and the value of the
1005 The CPU and wall clock times are printed, and the value of the
966 expression (if any) is returned. Note that under Win32, system time
1006 expression (if any) is returned. Note that under Win32, system time
967 is always reported as 0, since it can not be measured.
1007 is always reported as 0, since it can not be measured.
968
1008
969 This function can be used both as a line and cell magic:
1009 This function can be used both as a line and cell magic:
970
1010
971 - In line mode you can time a single-line statement (though multiple
1011 - In line mode you can time a single-line statement (though multiple
972 ones can be chained with using semicolons).
1012 ones can be chained with using semicolons).
973
1013
974 - In cell mode, you can time the cell body (a directly
1014 - In cell mode, you can time the cell body (a directly
975 following statement raises an error).
1015 following statement raises an error).
976
1016
977 This function provides very basic timing functionality. Use the timeit
1017 This function provides very basic timing functionality. Use the timeit
978 magic for more controll over the measurement.
1018 magic for more controll over the measurement.
979
1019
980 Examples
1020 Examples
981 --------
1021 --------
982 ::
1022 ::
983
1023
984 In [1]: %time 2**128
1024 In [1]: %time 2**128
985 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1025 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
986 Wall time: 0.00
1026 Wall time: 0.00
987 Out[1]: 340282366920938463463374607431768211456L
1027 Out[1]: 340282366920938463463374607431768211456L
988
1028
989 In [2]: n = 1000000
1029 In [2]: n = 1000000
990
1030
991 In [3]: %time sum(range(n))
1031 In [3]: %time sum(range(n))
992 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1032 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
993 Wall time: 1.37
1033 Wall time: 1.37
994 Out[3]: 499999500000L
1034 Out[3]: 499999500000L
995
1035
996 In [4]: %time print 'hello world'
1036 In [4]: %time print 'hello world'
997 hello world
1037 hello world
998 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1038 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
999 Wall time: 0.00
1039 Wall time: 0.00
1000
1040
1001 Note that the time needed by Python to compile the given expression
1041 Note that the time needed by Python to compile the given expression
1002 will be reported if it is more than 0.1s. In this example, the
1042 will be reported if it is more than 0.1s. In this example, the
1003 actual exponentiation is done by Python at compilation time, so while
1043 actual exponentiation is done by Python at compilation time, so while
1004 the expression can take a noticeable amount of time to compute, that
1044 the expression can take a noticeable amount of time to compute, that
1005 time is purely due to the compilation:
1045 time is purely due to the compilation:
1006
1046
1007 In [5]: %time 3**9999;
1047 In [5]: %time 3**9999;
1008 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1048 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1009 Wall time: 0.00 s
1049 Wall time: 0.00 s
1010
1050
1011 In [6]: %time 3**999999;
1051 In [6]: %time 3**999999;
1012 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1052 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1013 Wall time: 0.00 s
1053 Wall time: 0.00 s
1014 Compiler : 0.78 s
1054 Compiler : 0.78 s
1015 """
1055 """
1016
1056
1017 # fail immediately if the given expression can't be compiled
1057 # fail immediately if the given expression can't be compiled
1018
1058
1019 if line and cell:
1059 if line and cell:
1020 raise UsageError("Can't use statement directly after '%%time'!")
1060 raise UsageError("Can't use statement directly after '%%time'!")
1021
1061
1022 if cell:
1062 if cell:
1023 expr = self.shell.input_transformer_manager.transform_cell(cell)
1063 expr = self.shell.input_transformer_manager.transform_cell(cell)
1024 else:
1064 else:
1025 expr = self.shell.input_transformer_manager.transform_cell(line)
1065 expr = self.shell.input_transformer_manager.transform_cell(line)
1026
1066
1027 # Minimum time above which parse time will be reported
1067 # Minimum time above which parse time will be reported
1028 tp_min = 0.1
1068 tp_min = 0.1
1029
1069
1030 t0 = clock()
1070 t0 = clock()
1031 expr_ast = ast.parse(expr)
1071 expr_ast = ast.parse(expr)
1032 tp = clock()-t0
1072 tp = clock()-t0
1033
1073
1034 # Apply AST transformations
1074 # Apply AST transformations
1035 expr_ast = self.shell.transform_ast(expr_ast)
1075 expr_ast = self.shell.transform_ast(expr_ast)
1036
1076
1037 # Minimum time above which compilation time will be reported
1077 # Minimum time above which compilation time will be reported
1038 tc_min = 0.1
1078 tc_min = 0.1
1039
1079
1040 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1080 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1041 mode = 'eval'
1081 mode = 'eval'
1042 source = '<timed eval>'
1082 source = '<timed eval>'
1043 expr_ast = ast.Expression(expr_ast.body[0].value)
1083 expr_ast = ast.Expression(expr_ast.body[0].value)
1044 else:
1084 else:
1045 mode = 'exec'
1085 mode = 'exec'
1046 source = '<timed exec>'
1086 source = '<timed exec>'
1047 t0 = clock()
1087 t0 = clock()
1048 code = compile(expr_ast, source, mode)
1088 code = compile(expr_ast, source, mode)
1049 tc = clock()-t0
1089 tc = clock()-t0
1050
1090
1051 # skew measurement as little as possible
1091 # skew measurement as little as possible
1052 glob = self.shell.user_ns
1092 glob = self.shell.user_ns
1053 wtime = time.time
1093 wtime = time.time
1054 # time execution
1094 # time execution
1055 wall_st = wtime()
1095 wall_st = wtime()
1056 if mode=='eval':
1096 if mode=='eval':
1057 st = clock2()
1097 st = clock2()
1058 out = eval(code, glob, local_ns)
1098 out = eval(code, glob, local_ns)
1059 end = clock2()
1099 end = clock2()
1060 else:
1100 else:
1061 st = clock2()
1101 st = clock2()
1062 exec code in glob, local_ns
1102 exec code in glob, local_ns
1063 end = clock2()
1103 end = clock2()
1064 out = None
1104 out = None
1065 wall_end = wtime()
1105 wall_end = wtime()
1066 # Compute actual times and report
1106 # Compute actual times and report
1067 wall_time = wall_end-wall_st
1107 wall_time = wall_end-wall_st
1068 cpu_user = end[0]-st[0]
1108 cpu_user = end[0]-st[0]
1069 cpu_sys = end[1]-st[1]
1109 cpu_sys = end[1]-st[1]
1070 cpu_tot = cpu_user+cpu_sys
1110 cpu_tot = cpu_user+cpu_sys
1071 # On windows cpu_sys is always zero, so no new information to the next print
1111 # On windows cpu_sys is always zero, so no new information to the next print
1072 if sys.platform != 'win32':
1112 if sys.platform != 'win32':
1073 print "CPU times: user %s, sys: %s, total: %s" % \
1113 print "CPU times: user %s, sys: %s, total: %s" % \
1074 (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot))
1114 (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot))
1075 print "Wall time: %s" % _format_time(wall_time)
1115 print "Wall time: %s" % _format_time(wall_time)
1076 if tc > tc_min:
1116 if tc > tc_min:
1077 print "Compiler : %s" % _format_time(tc)
1117 print "Compiler : %s" % _format_time(tc)
1078 if tp > tp_min:
1118 if tp > tp_min:
1079 print "Parser : %s" % _format_time(tp)
1119 print "Parser : %s" % _format_time(tp)
1080 return out
1120 return out
1081
1121
1082 @skip_doctest
1122 @skip_doctest
1083 @line_magic
1123 @line_magic
1084 def macro(self, parameter_s=''):
1124 def macro(self, parameter_s=''):
1085 """Define a macro for future re-execution. It accepts ranges of history,
1125 """Define a macro for future re-execution. It accepts ranges of history,
1086 filenames or string objects.
1126 filenames or string objects.
1087
1127
1088 Usage:\\
1128 Usage:\\
1089 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1129 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1090
1130
1091 Options:
1131 Options:
1092
1132
1093 -r: use 'raw' input. By default, the 'processed' history is used,
1133 -r: use 'raw' input. By default, the 'processed' history is used,
1094 so that magics are loaded in their transformed version to valid
1134 so that magics are loaded in their transformed version to valid
1095 Python. If this option is given, the raw input as typed at the
1135 Python. If this option is given, the raw input as typed at the
1096 command line is used instead.
1136 command line is used instead.
1097
1137
1098 -q: quiet macro definition. By default, a tag line is printed
1138 -q: quiet macro definition. By default, a tag line is printed
1099 to indicate the macro has been created, and then the contents of
1139 to indicate the macro has been created, and then the contents of
1100 the macro are printed. If this option is given, then no printout
1140 the macro are printed. If this option is given, then no printout
1101 is produced once the macro is created.
1141 is produced once the macro is created.
1102
1142
1103 This will define a global variable called `name` which is a string
1143 This will define a global variable called `name` which is a string
1104 made of joining the slices and lines you specify (n1,n2,... numbers
1144 made of joining the slices and lines you specify (n1,n2,... numbers
1105 above) from your input history into a single string. This variable
1145 above) from your input history into a single string. This variable
1106 acts like an automatic function which re-executes those lines as if
1146 acts like an automatic function which re-executes those lines as if
1107 you had typed them. You just type 'name' at the prompt and the code
1147 you had typed them. You just type 'name' at the prompt and the code
1108 executes.
1148 executes.
1109
1149
1110 The syntax for indicating input ranges is described in %history.
1150 The syntax for indicating input ranges is described in %history.
1111
1151
1112 Note: as a 'hidden' feature, you can also use traditional python slice
1152 Note: as a 'hidden' feature, you can also use traditional python slice
1113 notation, where N:M means numbers N through M-1.
1153 notation, where N:M means numbers N through M-1.
1114
1154
1115 For example, if your history contains (print using %hist -n )::
1155 For example, if your history contains (print using %hist -n )::
1116
1156
1117 44: x=1
1157 44: x=1
1118 45: y=3
1158 45: y=3
1119 46: z=x+y
1159 46: z=x+y
1120 47: print x
1160 47: print x
1121 48: a=5
1161 48: a=5
1122 49: print 'x',x,'y',y
1162 49: print 'x',x,'y',y
1123
1163
1124 you can create a macro with lines 44 through 47 (included) and line 49
1164 you can create a macro with lines 44 through 47 (included) and line 49
1125 called my_macro with::
1165 called my_macro with::
1126
1166
1127 In [55]: %macro my_macro 44-47 49
1167 In [55]: %macro my_macro 44-47 49
1128
1168
1129 Now, typing `my_macro` (without quotes) will re-execute all this code
1169 Now, typing `my_macro` (without quotes) will re-execute all this code
1130 in one pass.
1170 in one pass.
1131
1171
1132 You don't need to give the line-numbers in order, and any given line
1172 You don't need to give the line-numbers in order, and any given line
1133 number can appear multiple times. You can assemble macros with any
1173 number can appear multiple times. You can assemble macros with any
1134 lines from your input history in any order.
1174 lines from your input history in any order.
1135
1175
1136 The macro is a simple object which holds its value in an attribute,
1176 The macro is a simple object which holds its value in an attribute,
1137 but IPython's display system checks for macros and executes them as
1177 but IPython's display system checks for macros and executes them as
1138 code instead of printing them when you type their name.
1178 code instead of printing them when you type their name.
1139
1179
1140 You can view a macro's contents by explicitly printing it with::
1180 You can view a macro's contents by explicitly printing it with::
1141
1181
1142 print macro_name
1182 print macro_name
1143
1183
1144 """
1184 """
1145 opts,args = self.parse_options(parameter_s,'rq',mode='list')
1185 opts,args = self.parse_options(parameter_s,'rq',mode='list')
1146 if not args: # List existing macros
1186 if not args: # List existing macros
1147 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
1187 return sorted(k for k,v in self.shell.user_ns.iteritems() if\
1148 isinstance(v, Macro))
1188 isinstance(v, Macro))
1149 if len(args) == 1:
1189 if len(args) == 1:
1150 raise UsageError(
1190 raise UsageError(
1151 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1191 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1152 name, codefrom = args[0], " ".join(args[1:])
1192 name, codefrom = args[0], " ".join(args[1:])
1153
1193
1154 #print 'rng',ranges # dbg
1194 #print 'rng',ranges # dbg
1155 try:
1195 try:
1156 lines = self.shell.find_user_code(codefrom, 'r' in opts)
1196 lines = self.shell.find_user_code(codefrom, 'r' in opts)
1157 except (ValueError, TypeError) as e:
1197 except (ValueError, TypeError) as e:
1158 print e.args[0]
1198 print e.args[0]
1159 return
1199 return
1160 macro = Macro(lines)
1200 macro = Macro(lines)
1161 self.shell.define_macro(name, macro)
1201 self.shell.define_macro(name, macro)
1162 if not ( 'q' in opts) :
1202 if not ( 'q' in opts) :
1163 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1203 print 'Macro `%s` created. To execute, type its name (without quotes).' % name
1164 print '=== Macro contents: ==='
1204 print '=== Macro contents: ==='
1165 print macro,
1205 print macro,
1166
1206
1167 @magic_arguments.magic_arguments()
1207 @magic_arguments.magic_arguments()
1168 @magic_arguments.argument('output', type=str, default='', nargs='?',
1208 @magic_arguments.argument('output', type=str, default='', nargs='?',
1169 help="""The name of the variable in which to store output.
1209 help="""The name of the variable in which to store output.
1170 This is a utils.io.CapturedIO object with stdout/err attributes
1210 This is a utils.io.CapturedIO object with stdout/err attributes
1171 for the text of the captured output.
1211 for the text of the captured output.
1172
1212
1173 CapturedOutput also has a show() method for displaying the output,
1213 CapturedOutput also has a show() method for displaying the output,
1174 and __call__ as well, so you can use that to quickly display the
1214 and __call__ as well, so you can use that to quickly display the
1175 output.
1215 output.
1176
1216
1177 If unspecified, captured output is discarded.
1217 If unspecified, captured output is discarded.
1178 """
1218 """
1179 )
1219 )
1180 @magic_arguments.argument('--no-stderr', action="store_true",
1220 @magic_arguments.argument('--no-stderr', action="store_true",
1181 help="""Don't capture stderr."""
1221 help="""Don't capture stderr."""
1182 )
1222 )
1183 @magic_arguments.argument('--no-stdout', action="store_true",
1223 @magic_arguments.argument('--no-stdout', action="store_true",
1184 help="""Don't capture stdout."""
1224 help="""Don't capture stdout."""
1185 )
1225 )
1186 @magic_arguments.argument('--no-display', action="store_true",
1226 @magic_arguments.argument('--no-display', action="store_true",
1187 help="""Don't capture IPython's rich display."""
1227 help="""Don't capture IPython's rich display."""
1188 )
1228 )
1189 @cell_magic
1229 @cell_magic
1190 def capture(self, line, cell):
1230 def capture(self, line, cell):
1191 """run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
1231 """run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
1192 args = magic_arguments.parse_argstring(self.capture, line)
1232 args = magic_arguments.parse_argstring(self.capture, line)
1193 out = not args.no_stdout
1233 out = not args.no_stdout
1194 err = not args.no_stderr
1234 err = not args.no_stderr
1195 disp = not args.no_display
1235 disp = not args.no_display
1196 with capture_output(out, err, disp) as io:
1236 with capture_output(out, err, disp) as io:
1197 self.shell.run_cell(cell)
1237 self.shell.run_cell(cell)
1198 if args.output:
1238 if args.output:
1199 self.shell.user_ns[args.output] = io
1239 self.shell.user_ns[args.output] = io
1200
1240
1201 def parse_breakpoint(text, current_file):
1241 def parse_breakpoint(text, current_file):
1202 '''Returns (file, line) for file:line and (current_file, line) for line'''
1242 '''Returns (file, line) for file:line and (current_file, line) for line'''
1203 colon = text.find(':')
1243 colon = text.find(':')
1204 if colon == -1:
1244 if colon == -1:
1205 return current_file, int(text)
1245 return current_file, int(text)
1206 else:
1246 else:
1207 return text[:colon], int(text[colon+1:])
1247 return text[:colon], int(text[colon+1:])
1208
1248
1209 def _format_time(timespan, precision=3):
1249 def _format_time(timespan, precision=3):
1210 """Formats the timespan in a human readable form"""
1250 """Formats the timespan in a human readable form"""
1211 import math
1251 import math
1212
1252
1213 if timespan >= 60.0:
1253 if timespan >= 60.0:
1214 # we have more than a minute, format that in a human readable form
1254 # we have more than a minute, format that in a human readable form
1215 # Idea from http://snipplr.com/view/5713/
1255 # Idea from http://snipplr.com/view/5713/
1216 parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
1256 parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
1217 time = []
1257 time = []
1218 leftover = timespan
1258 leftover = timespan
1219 for suffix, length in parts:
1259 for suffix, length in parts:
1220 value = int(leftover / length)
1260 value = int(leftover / length)
1221 if value > 0:
1261 if value > 0:
1222 leftover = leftover % length
1262 leftover = leftover % length
1223 time.append(u'%s%s' % (str(value), suffix))
1263 time.append(u'%s%s' % (str(value), suffix))
1224 if leftover < 1:
1264 if leftover < 1:
1225 break
1265 break
1226 return " ".join(time)
1266 return " ".join(time)
1227
1267
1228
1268
1229 # Unfortunately the unicode 'micro' symbol can cause problems in
1269 # Unfortunately the unicode 'micro' symbol can cause problems in
1230 # certain terminals.
1270 # certain terminals.
1231 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1271 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1232 # Try to prevent crashes by being more secure than it needs to
1272 # Try to prevent crashes by being more secure than it needs to
1233 # E.g. eclipse is able to print a Β΅, but has no sys.stdout.encoding set.
1273 # E.g. eclipse is able to print a Β΅, but has no sys.stdout.encoding set.
1234 units = [u"s", u"ms",u'us',"ns"] # the save value
1274 units = [u"s", u"ms",u'us',"ns"] # the save value
1235 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
1275 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
1236 try:
1276 try:
1237 u'\xb5'.encode(sys.stdout.encoding)
1277 u'\xb5'.encode(sys.stdout.encoding)
1238 units = [u"s", u"ms",u'\xb5s',"ns"]
1278 units = [u"s", u"ms",u'\xb5s',"ns"]
1239 except:
1279 except:
1240 pass
1280 pass
1241 scaling = [1, 1e3, 1e6, 1e9]
1281 scaling = [1, 1e3, 1e6, 1e9]
1242
1282
1243 if timespan > 0.0:
1283 if timespan > 0.0:
1244 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1284 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1245 else:
1285 else:
1246 order = 3
1286 order = 3
1247 ret = u"%.*g %s" % (precision, timespan * scaling[order], units[order])
1287 ret = u"%.*g %s" % (precision, timespan * scaling[order], units[order])
1248 return ret
1288 return ret
@@ -1,881 +1,895 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tests for various magic functions.
2 """Tests for various magic functions.
3
3
4 Needs to be run by nose (to make ipython session available).
4 Needs to be run by nose (to make ipython session available).
5 """
5 """
6 from __future__ import absolute_import
6 from __future__ import absolute_import
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Imports
9 # Imports
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
11
12 import io
12 import io
13 import os
13 import os
14 import sys
14 import sys
15 from StringIO import StringIO
15 from StringIO import StringIO
16 from unittest import TestCase
16 from unittest import TestCase
17
17
18 try:
18 try:
19 from importlib import invalidate_caches # Required from Python 3.3
19 from importlib import invalidate_caches # Required from Python 3.3
20 except ImportError:
20 except ImportError:
21 def invalidate_caches():
21 def invalidate_caches():
22 pass
22 pass
23
23
24 import nose.tools as nt
24 import nose.tools as nt
25
25
26 from IPython.core import magic
26 from IPython.core import magic
27 from IPython.core.magic import (Magics, magics_class, line_magic,
27 from IPython.core.magic import (Magics, magics_class, line_magic,
28 cell_magic, line_cell_magic,
28 cell_magic, line_cell_magic,
29 register_line_magic, register_cell_magic,
29 register_line_magic, register_cell_magic,
30 register_line_cell_magic)
30 register_line_cell_magic)
31 from IPython.core.magics import execution, script, code
31 from IPython.core.magics import execution, script, code
32 from IPython.nbformat.v3.tests.nbexamples import nb0
32 from IPython.nbformat.v3.tests.nbexamples import nb0
33 from IPython.nbformat import current
33 from IPython.nbformat import current
34 from IPython.testing import decorators as dec
34 from IPython.testing import decorators as dec
35 from IPython.testing import tools as tt
35 from IPython.testing import tools as tt
36 from IPython.utils import py3compat
36 from IPython.utils import py3compat
37 from IPython.utils.io import capture_output
37 from IPython.utils.io import capture_output
38 from IPython.utils.tempdir import TemporaryDirectory
38 from IPython.utils.tempdir import TemporaryDirectory
39 from IPython.utils.process import find_cmd
39 from IPython.utils.process import find_cmd
40
40
41 #-----------------------------------------------------------------------------
41 #-----------------------------------------------------------------------------
42 # Test functions begin
42 # Test functions begin
43 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
44
44
45 @magic.magics_class
45 @magic.magics_class
46 class DummyMagics(magic.Magics): pass
46 class DummyMagics(magic.Magics): pass
47
47
48 def test_rehashx():
48 def test_rehashx():
49 # clear up everything
49 # clear up everything
50 _ip = get_ipython()
50 _ip = get_ipython()
51 _ip.alias_manager.alias_table.clear()
51 _ip.alias_manager.alias_table.clear()
52 del _ip.db['syscmdlist']
52 del _ip.db['syscmdlist']
53
53
54 _ip.magic('rehashx')
54 _ip.magic('rehashx')
55 # Practically ALL ipython development systems will have more than 10 aliases
55 # Practically ALL ipython development systems will have more than 10 aliases
56
56
57 nt.assert_true(len(_ip.alias_manager.alias_table) > 10)
57 nt.assert_true(len(_ip.alias_manager.alias_table) > 10)
58 for key, val in _ip.alias_manager.alias_table.iteritems():
58 for key, val in _ip.alias_manager.alias_table.iteritems():
59 # we must strip dots from alias names
59 # we must strip dots from alias names
60 nt.assert_not_in('.', key)
60 nt.assert_not_in('.', key)
61
61
62 # rehashx must fill up syscmdlist
62 # rehashx must fill up syscmdlist
63 scoms = _ip.db['syscmdlist']
63 scoms = _ip.db['syscmdlist']
64 nt.assert_true(len(scoms) > 10)
64 nt.assert_true(len(scoms) > 10)
65
65
66
66
67 def test_magic_parse_options():
67 def test_magic_parse_options():
68 """Test that we don't mangle paths when parsing magic options."""
68 """Test that we don't mangle paths when parsing magic options."""
69 ip = get_ipython()
69 ip = get_ipython()
70 path = 'c:\\x'
70 path = 'c:\\x'
71 m = DummyMagics(ip)
71 m = DummyMagics(ip)
72 opts = m.parse_options('-f %s' % path,'f:')[0]
72 opts = m.parse_options('-f %s' % path,'f:')[0]
73 # argv splitting is os-dependent
73 # argv splitting is os-dependent
74 if os.name == 'posix':
74 if os.name == 'posix':
75 expected = 'c:x'
75 expected = 'c:x'
76 else:
76 else:
77 expected = path
77 expected = path
78 nt.assert_equal(opts['f'], expected)
78 nt.assert_equal(opts['f'], expected)
79
79
80 def test_magic_parse_long_options():
80 def test_magic_parse_long_options():
81 """Magic.parse_options can handle --foo=bar long options"""
81 """Magic.parse_options can handle --foo=bar long options"""
82 ip = get_ipython()
82 ip = get_ipython()
83 m = DummyMagics(ip)
83 m = DummyMagics(ip)
84 opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=')
84 opts, _ = m.parse_options('--foo --bar=bubble', 'a', 'foo', 'bar=')
85 nt.assert_in('foo', opts)
85 nt.assert_in('foo', opts)
86 nt.assert_in('bar', opts)
86 nt.assert_in('bar', opts)
87 nt.assert_equal(opts['bar'], "bubble")
87 nt.assert_equal(opts['bar'], "bubble")
88
88
89
89
90 @dec.skip_without('sqlite3')
90 @dec.skip_without('sqlite3')
91 def doctest_hist_f():
91 def doctest_hist_f():
92 """Test %hist -f with temporary filename.
92 """Test %hist -f with temporary filename.
93
93
94 In [9]: import tempfile
94 In [9]: import tempfile
95
95
96 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
96 In [10]: tfile = tempfile.mktemp('.py','tmp-ipython-')
97
97
98 In [11]: %hist -nl -f $tfile 3
98 In [11]: %hist -nl -f $tfile 3
99
99
100 In [13]: import os; os.unlink(tfile)
100 In [13]: import os; os.unlink(tfile)
101 """
101 """
102
102
103
103
104 @dec.skip_without('sqlite3')
104 @dec.skip_without('sqlite3')
105 def doctest_hist_r():
105 def doctest_hist_r():
106 """Test %hist -r
106 """Test %hist -r
107
107
108 XXX - This test is not recording the output correctly. For some reason, in
108 XXX - This test is not recording the output correctly. For some reason, in
109 testing mode the raw history isn't getting populated. No idea why.
109 testing mode the raw history isn't getting populated. No idea why.
110 Disabling the output checking for now, though at least we do run it.
110 Disabling the output checking for now, though at least we do run it.
111
111
112 In [1]: 'hist' in _ip.lsmagic()
112 In [1]: 'hist' in _ip.lsmagic()
113 Out[1]: True
113 Out[1]: True
114
114
115 In [2]: x=1
115 In [2]: x=1
116
116
117 In [3]: %hist -rl 2
117 In [3]: %hist -rl 2
118 x=1 # random
118 x=1 # random
119 %hist -r 2
119 %hist -r 2
120 """
120 """
121
121
122
122
123 @dec.skip_without('sqlite3')
123 @dec.skip_without('sqlite3')
124 def doctest_hist_op():
124 def doctest_hist_op():
125 """Test %hist -op
125 """Test %hist -op
126
126
127 In [1]: class b(float):
127 In [1]: class b(float):
128 ...: pass
128 ...: pass
129 ...:
129 ...:
130
130
131 In [2]: class s(object):
131 In [2]: class s(object):
132 ...: def __str__(self):
132 ...: def __str__(self):
133 ...: return 's'
133 ...: return 's'
134 ...:
134 ...:
135
135
136 In [3]:
136 In [3]:
137
137
138 In [4]: class r(b):
138 In [4]: class r(b):
139 ...: def __repr__(self):
139 ...: def __repr__(self):
140 ...: return 'r'
140 ...: return 'r'
141 ...:
141 ...:
142
142
143 In [5]: class sr(s,r): pass
143 In [5]: class sr(s,r): pass
144 ...:
144 ...:
145
145
146 In [6]:
146 In [6]:
147
147
148 In [7]: bb=b()
148 In [7]: bb=b()
149
149
150 In [8]: ss=s()
150 In [8]: ss=s()
151
151
152 In [9]: rr=r()
152 In [9]: rr=r()
153
153
154 In [10]: ssrr=sr()
154 In [10]: ssrr=sr()
155
155
156 In [11]: 4.5
156 In [11]: 4.5
157 Out[11]: 4.5
157 Out[11]: 4.5
158
158
159 In [12]: str(ss)
159 In [12]: str(ss)
160 Out[12]: 's'
160 Out[12]: 's'
161
161
162 In [13]:
162 In [13]:
163
163
164 In [14]: %hist -op
164 In [14]: %hist -op
165 >>> class b:
165 >>> class b:
166 ... pass
166 ... pass
167 ...
167 ...
168 >>> class s(b):
168 >>> class s(b):
169 ... def __str__(self):
169 ... def __str__(self):
170 ... return 's'
170 ... return 's'
171 ...
171 ...
172 >>>
172 >>>
173 >>> class r(b):
173 >>> class r(b):
174 ... def __repr__(self):
174 ... def __repr__(self):
175 ... return 'r'
175 ... return 'r'
176 ...
176 ...
177 >>> class sr(s,r): pass
177 >>> class sr(s,r): pass
178 >>>
178 >>>
179 >>> bb=b()
179 >>> bb=b()
180 >>> ss=s()
180 >>> ss=s()
181 >>> rr=r()
181 >>> rr=r()
182 >>> ssrr=sr()
182 >>> ssrr=sr()
183 >>> 4.5
183 >>> 4.5
184 4.5
184 4.5
185 >>> str(ss)
185 >>> str(ss)
186 's'
186 's'
187 >>>
187 >>>
188 """
188 """
189
189
190
190
191 @dec.skip_without('sqlite3')
191 @dec.skip_without('sqlite3')
192 def test_macro():
192 def test_macro():
193 ip = get_ipython()
193 ip = get_ipython()
194 ip.history_manager.reset() # Clear any existing history.
194 ip.history_manager.reset() # Clear any existing history.
195 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
195 cmds = ["a=1", "def b():\n return a**2", "print(a,b())"]
196 for i, cmd in enumerate(cmds, start=1):
196 for i, cmd in enumerate(cmds, start=1):
197 ip.history_manager.store_inputs(i, cmd)
197 ip.history_manager.store_inputs(i, cmd)
198 ip.magic("macro test 1-3")
198 ip.magic("macro test 1-3")
199 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
199 nt.assert_equal(ip.user_ns["test"].value, "\n".join(cmds)+"\n")
200
200
201 # List macros
201 # List macros
202 nt.assert_in("test", ip.magic("macro"))
202 nt.assert_in("test", ip.magic("macro"))
203
203
204
204
205 @dec.skip_without('sqlite3')
205 @dec.skip_without('sqlite3')
206 def test_macro_run():
206 def test_macro_run():
207 """Test that we can run a multi-line macro successfully."""
207 """Test that we can run a multi-line macro successfully."""
208 ip = get_ipython()
208 ip = get_ipython()
209 ip.history_manager.reset()
209 ip.history_manager.reset()
210 cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"),
210 cmds = ["a=10", "a+=1", py3compat.doctest_refactor_print("print a"),
211 "%macro test 2-3"]
211 "%macro test 2-3"]
212 for cmd in cmds:
212 for cmd in cmds:
213 ip.run_cell(cmd, store_history=True)
213 ip.run_cell(cmd, store_history=True)
214 nt.assert_equal(ip.user_ns["test"].value,
214 nt.assert_equal(ip.user_ns["test"].value,
215 py3compat.doctest_refactor_print("a+=1\nprint a\n"))
215 py3compat.doctest_refactor_print("a+=1\nprint a\n"))
216 with tt.AssertPrints("12"):
216 with tt.AssertPrints("12"):
217 ip.run_cell("test")
217 ip.run_cell("test")
218 with tt.AssertPrints("13"):
218 with tt.AssertPrints("13"):
219 ip.run_cell("test")
219 ip.run_cell("test")
220
220
221
221
222 def test_magic_magic():
222 def test_magic_magic():
223 """Test %magic"""
223 """Test %magic"""
224 ip = get_ipython()
224 ip = get_ipython()
225 with capture_output() as captured:
225 with capture_output() as captured:
226 ip.magic("magic")
226 ip.magic("magic")
227
227
228 stdout = captured.stdout
228 stdout = captured.stdout
229 nt.assert_in('%magic', stdout)
229 nt.assert_in('%magic', stdout)
230 nt.assert_in('IPython', stdout)
230 nt.assert_in('IPython', stdout)
231 nt.assert_in('Available', stdout)
231 nt.assert_in('Available', stdout)
232
232
233
233
234 @dec.skipif_not_numpy
234 @dec.skipif_not_numpy
235 def test_numpy_reset_array_undec():
235 def test_numpy_reset_array_undec():
236 "Test '%reset array' functionality"
236 "Test '%reset array' functionality"
237 _ip.ex('import numpy as np')
237 _ip.ex('import numpy as np')
238 _ip.ex('a = np.empty(2)')
238 _ip.ex('a = np.empty(2)')
239 nt.assert_in('a', _ip.user_ns)
239 nt.assert_in('a', _ip.user_ns)
240 _ip.magic('reset -f array')
240 _ip.magic('reset -f array')
241 nt.assert_not_in('a', _ip.user_ns)
241 nt.assert_not_in('a', _ip.user_ns)
242
242
243 def test_reset_out():
243 def test_reset_out():
244 "Test '%reset out' magic"
244 "Test '%reset out' magic"
245 _ip.run_cell("parrot = 'dead'", store_history=True)
245 _ip.run_cell("parrot = 'dead'", store_history=True)
246 # test '%reset -f out', make an Out prompt
246 # test '%reset -f out', make an Out prompt
247 _ip.run_cell("parrot", store_history=True)
247 _ip.run_cell("parrot", store_history=True)
248 nt.assert_true('dead' in [_ip.user_ns[x] for x in '_','__','___'])
248 nt.assert_true('dead' in [_ip.user_ns[x] for x in '_','__','___'])
249 _ip.magic('reset -f out')
249 _ip.magic('reset -f out')
250 nt.assert_false('dead' in [_ip.user_ns[x] for x in '_','__','___'])
250 nt.assert_false('dead' in [_ip.user_ns[x] for x in '_','__','___'])
251 nt.assert_equal(len(_ip.user_ns['Out']), 0)
251 nt.assert_equal(len(_ip.user_ns['Out']), 0)
252
252
253 def test_reset_in():
253 def test_reset_in():
254 "Test '%reset in' magic"
254 "Test '%reset in' magic"
255 # test '%reset -f in'
255 # test '%reset -f in'
256 _ip.run_cell("parrot", store_history=True)
256 _ip.run_cell("parrot", store_history=True)
257 nt.assert_true('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
257 nt.assert_true('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
258 _ip.magic('%reset -f in')
258 _ip.magic('%reset -f in')
259 nt.assert_false('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
259 nt.assert_false('parrot' in [_ip.user_ns[x] for x in '_i','_ii','_iii'])
260 nt.assert_equal(len(set(_ip.user_ns['In'])), 1)
260 nt.assert_equal(len(set(_ip.user_ns['In'])), 1)
261
261
262 def test_reset_dhist():
262 def test_reset_dhist():
263 "Test '%reset dhist' magic"
263 "Test '%reset dhist' magic"
264 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
264 _ip.run_cell("tmp = [d for d in _dh]") # copy before clearing
265 _ip.magic('cd ' + os.path.dirname(nt.__file__))
265 _ip.magic('cd ' + os.path.dirname(nt.__file__))
266 _ip.magic('cd -')
266 _ip.magic('cd -')
267 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
267 nt.assert_true(len(_ip.user_ns['_dh']) > 0)
268 _ip.magic('reset -f dhist')
268 _ip.magic('reset -f dhist')
269 nt.assert_equal(len(_ip.user_ns['_dh']), 0)
269 nt.assert_equal(len(_ip.user_ns['_dh']), 0)
270 _ip.run_cell("_dh = [d for d in tmp]") #restore
270 _ip.run_cell("_dh = [d for d in tmp]") #restore
271
271
272 def test_reset_in_length():
272 def test_reset_in_length():
273 "Test that '%reset in' preserves In[] length"
273 "Test that '%reset in' preserves In[] length"
274 _ip.run_cell("print 'foo'")
274 _ip.run_cell("print 'foo'")
275 _ip.run_cell("reset -f in")
275 _ip.run_cell("reset -f in")
276 nt.assert_equal(len(_ip.user_ns['In']), _ip.displayhook.prompt_count+1)
276 nt.assert_equal(len(_ip.user_ns['In']), _ip.displayhook.prompt_count+1)
277
277
278 def test_tb_syntaxerror():
278 def test_tb_syntaxerror():
279 """test %tb after a SyntaxError"""
279 """test %tb after a SyntaxError"""
280 ip = get_ipython()
280 ip = get_ipython()
281 ip.run_cell("for")
281 ip.run_cell("for")
282
282
283 # trap and validate stdout
283 # trap and validate stdout
284 save_stdout = sys.stdout
284 save_stdout = sys.stdout
285 try:
285 try:
286 sys.stdout = StringIO()
286 sys.stdout = StringIO()
287 ip.run_cell("%tb")
287 ip.run_cell("%tb")
288 out = sys.stdout.getvalue()
288 out = sys.stdout.getvalue()
289 finally:
289 finally:
290 sys.stdout = save_stdout
290 sys.stdout = save_stdout
291 # trim output, and only check the last line
291 # trim output, and only check the last line
292 last_line = out.rstrip().splitlines()[-1].strip()
292 last_line = out.rstrip().splitlines()[-1].strip()
293 nt.assert_equal(last_line, "SyntaxError: invalid syntax")
293 nt.assert_equal(last_line, "SyntaxError: invalid syntax")
294
294
295
295
296 def test_time():
296 def test_time():
297 ip = get_ipython()
297 ip = get_ipython()
298
298
299 with tt.AssertPrints("Wall time: "):
299 with tt.AssertPrints("Wall time: "):
300 ip.run_cell("%time None")
300 ip.run_cell("%time None")
301
301
302 ip.run_cell("def f(kmjy):\n"
302 ip.run_cell("def f(kmjy):\n"
303 " %time print (2*kmjy)")
303 " %time print (2*kmjy)")
304
304
305 with tt.AssertPrints("Wall time: "):
305 with tt.AssertPrints("Wall time: "):
306 with tt.AssertPrints("hihi", suppress=False):
306 with tt.AssertPrints("hihi", suppress=False):
307 ip.run_cell("f('hi')")
307 ip.run_cell("f('hi')")
308
308
309
309
310 @dec.skip_win32
310 @dec.skip_win32
311 def test_time2():
311 def test_time2():
312 ip = get_ipython()
312 ip = get_ipython()
313
313
314 with tt.AssertPrints("CPU times: user "):
314 with tt.AssertPrints("CPU times: user "):
315 ip.run_cell("%time None")
315 ip.run_cell("%time None")
316
316
317 def test_time3():
317 def test_time3():
318 """Erroneous magic function calls, issue gh-3334"""
318 """Erroneous magic function calls, issue gh-3334"""
319 ip = get_ipython()
319 ip = get_ipython()
320 ip.user_ns.pop('run', None)
320 ip.user_ns.pop('run', None)
321
321
322 with tt.AssertNotPrints("not found", channel='stderr'):
322 with tt.AssertNotPrints("not found", channel='stderr'):
323 ip.run_cell("%%time\n"
323 ip.run_cell("%%time\n"
324 "run = 0\n"
324 "run = 0\n"
325 "run += 1")
325 "run += 1")
326
326
327 def test_doctest_mode():
327 def test_doctest_mode():
328 "Toggle doctest_mode twice, it should be a no-op and run without error"
328 "Toggle doctest_mode twice, it should be a no-op and run without error"
329 _ip.magic('doctest_mode')
329 _ip.magic('doctest_mode')
330 _ip.magic('doctest_mode')
330 _ip.magic('doctest_mode')
331
331
332
332
333 def test_parse_options():
333 def test_parse_options():
334 """Tests for basic options parsing in magics."""
334 """Tests for basic options parsing in magics."""
335 # These are only the most minimal of tests, more should be added later. At
335 # These are only the most minimal of tests, more should be added later. At
336 # the very least we check that basic text/unicode calls work OK.
336 # the very least we check that basic text/unicode calls work OK.
337 m = DummyMagics(_ip)
337 m = DummyMagics(_ip)
338 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
338 nt.assert_equal(m.parse_options('foo', '')[1], 'foo')
339 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
339 nt.assert_equal(m.parse_options(u'foo', '')[1], u'foo')
340
340
341
341
342 def test_dirops():
342 def test_dirops():
343 """Test various directory handling operations."""
343 """Test various directory handling operations."""
344 # curpath = lambda :os.path.splitdrive(os.getcwdu())[1].replace('\\','/')
344 # curpath = lambda :os.path.splitdrive(os.getcwdu())[1].replace('\\','/')
345 curpath = os.getcwdu
345 curpath = os.getcwdu
346 startdir = os.getcwdu()
346 startdir = os.getcwdu()
347 ipdir = os.path.realpath(_ip.ipython_dir)
347 ipdir = os.path.realpath(_ip.ipython_dir)
348 try:
348 try:
349 _ip.magic('cd "%s"' % ipdir)
349 _ip.magic('cd "%s"' % ipdir)
350 nt.assert_equal(curpath(), ipdir)
350 nt.assert_equal(curpath(), ipdir)
351 _ip.magic('cd -')
351 _ip.magic('cd -')
352 nt.assert_equal(curpath(), startdir)
352 nt.assert_equal(curpath(), startdir)
353 _ip.magic('pushd "%s"' % ipdir)
353 _ip.magic('pushd "%s"' % ipdir)
354 nt.assert_equal(curpath(), ipdir)
354 nt.assert_equal(curpath(), ipdir)
355 _ip.magic('popd')
355 _ip.magic('popd')
356 nt.assert_equal(curpath(), startdir)
356 nt.assert_equal(curpath(), startdir)
357 finally:
357 finally:
358 os.chdir(startdir)
358 os.chdir(startdir)
359
359
360
360
361 def test_xmode():
361 def test_xmode():
362 # Calling xmode three times should be a no-op
362 # Calling xmode three times should be a no-op
363 xmode = _ip.InteractiveTB.mode
363 xmode = _ip.InteractiveTB.mode
364 for i in range(3):
364 for i in range(3):
365 _ip.magic("xmode")
365 _ip.magic("xmode")
366 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
366 nt.assert_equal(_ip.InteractiveTB.mode, xmode)
367
367
368 def test_reset_hard():
368 def test_reset_hard():
369 monitor = []
369 monitor = []
370 class A(object):
370 class A(object):
371 def __del__(self):
371 def __del__(self):
372 monitor.append(1)
372 monitor.append(1)
373 def __repr__(self):
373 def __repr__(self):
374 return "<A instance>"
374 return "<A instance>"
375
375
376 _ip.user_ns["a"] = A()
376 _ip.user_ns["a"] = A()
377 _ip.run_cell("a")
377 _ip.run_cell("a")
378
378
379 nt.assert_equal(monitor, [])
379 nt.assert_equal(monitor, [])
380 _ip.magic("reset -f")
380 _ip.magic("reset -f")
381 nt.assert_equal(monitor, [1])
381 nt.assert_equal(monitor, [1])
382
382
383 class TestXdel(tt.TempFileMixin):
383 class TestXdel(tt.TempFileMixin):
384 def test_xdel(self):
384 def test_xdel(self):
385 """Test that references from %run are cleared by xdel."""
385 """Test that references from %run are cleared by xdel."""
386 src = ("class A(object):\n"
386 src = ("class A(object):\n"
387 " monitor = []\n"
387 " monitor = []\n"
388 " def __del__(self):\n"
388 " def __del__(self):\n"
389 " self.monitor.append(1)\n"
389 " self.monitor.append(1)\n"
390 "a = A()\n")
390 "a = A()\n")
391 self.mktmp(src)
391 self.mktmp(src)
392 # %run creates some hidden references...
392 # %run creates some hidden references...
393 _ip.magic("run %s" % self.fname)
393 _ip.magic("run %s" % self.fname)
394 # ... as does the displayhook.
394 # ... as does the displayhook.
395 _ip.run_cell("a")
395 _ip.run_cell("a")
396
396
397 monitor = _ip.user_ns["A"].monitor
397 monitor = _ip.user_ns["A"].monitor
398 nt.assert_equal(monitor, [])
398 nt.assert_equal(monitor, [])
399
399
400 _ip.magic("xdel a")
400 _ip.magic("xdel a")
401
401
402 # Check that a's __del__ method has been called.
402 # Check that a's __del__ method has been called.
403 nt.assert_equal(monitor, [1])
403 nt.assert_equal(monitor, [1])
404
404
405 def doctest_who():
405 def doctest_who():
406 """doctest for %who
406 """doctest for %who
407
407
408 In [1]: %reset -f
408 In [1]: %reset -f
409
409
410 In [2]: alpha = 123
410 In [2]: alpha = 123
411
411
412 In [3]: beta = 'beta'
412 In [3]: beta = 'beta'
413
413
414 In [4]: %who int
414 In [4]: %who int
415 alpha
415 alpha
416
416
417 In [5]: %who str
417 In [5]: %who str
418 beta
418 beta
419
419
420 In [6]: %whos
420 In [6]: %whos
421 Variable Type Data/Info
421 Variable Type Data/Info
422 ----------------------------
422 ----------------------------
423 alpha int 123
423 alpha int 123
424 beta str beta
424 beta str beta
425
425
426 In [7]: %who_ls
426 In [7]: %who_ls
427 Out[7]: ['alpha', 'beta']
427 Out[7]: ['alpha', 'beta']
428 """
428 """
429
429
430 def test_whos():
430 def test_whos():
431 """Check that whos is protected against objects where repr() fails."""
431 """Check that whos is protected against objects where repr() fails."""
432 class A(object):
432 class A(object):
433 def __repr__(self):
433 def __repr__(self):
434 raise Exception()
434 raise Exception()
435 _ip.user_ns['a'] = A()
435 _ip.user_ns['a'] = A()
436 _ip.magic("whos")
436 _ip.magic("whos")
437
437
438 @py3compat.u_format
438 @py3compat.u_format
439 def doctest_precision():
439 def doctest_precision():
440 """doctest for %precision
440 """doctest for %precision
441
441
442 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
442 In [1]: f = get_ipython().display_formatter.formatters['text/plain']
443
443
444 In [2]: %precision 5
444 In [2]: %precision 5
445 Out[2]: {u}'%.5f'
445 Out[2]: {u}'%.5f'
446
446
447 In [3]: f.float_format
447 In [3]: f.float_format
448 Out[3]: {u}'%.5f'
448 Out[3]: {u}'%.5f'
449
449
450 In [4]: %precision %e
450 In [4]: %precision %e
451 Out[4]: {u}'%e'
451 Out[4]: {u}'%e'
452
452
453 In [5]: f(3.1415927)
453 In [5]: f(3.1415927)
454 Out[5]: {u}'3.141593e+00'
454 Out[5]: {u}'3.141593e+00'
455 """
455 """
456
456
457 def test_psearch():
457 def test_psearch():
458 with tt.AssertPrints("dict.fromkeys"):
458 with tt.AssertPrints("dict.fromkeys"):
459 _ip.run_cell("dict.fr*?")
459 _ip.run_cell("dict.fr*?")
460
460
461 def test_timeit_shlex():
461 def test_timeit_shlex():
462 """test shlex issues with timeit (#1109)"""
462 """test shlex issues with timeit (#1109)"""
463 _ip.ex("def f(*a,**kw): pass")
463 _ip.ex("def f(*a,**kw): pass")
464 _ip.magic('timeit -n1 "this is a bug".count(" ")')
464 _ip.magic('timeit -n1 "this is a bug".count(" ")')
465 _ip.magic('timeit -r1 -n1 f(" ", 1)')
465 _ip.magic('timeit -r1 -n1 f(" ", 1)')
466 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
466 _ip.magic('timeit -r1 -n1 f(" ", 1, " ", 2, " ")')
467 _ip.magic('timeit -r1 -n1 ("a " + "b")')
467 _ip.magic('timeit -r1 -n1 ("a " + "b")')
468 _ip.magic('timeit -r1 -n1 f("a " + "b")')
468 _ip.magic('timeit -r1 -n1 f("a " + "b")')
469 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
469 _ip.magic('timeit -r1 -n1 f("a " + "b ")')
470
470
471
471
472 def test_timeit_arguments():
472 def test_timeit_arguments():
473 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
473 "Test valid timeit arguments, should not cause SyntaxError (GH #1269)"
474 _ip.magic("timeit ('#')")
474 _ip.magic("timeit ('#')")
475
475
476
476
477 def test_timeit_special_syntax():
477 def test_timeit_special_syntax():
478 "Test %%timeit with IPython special syntax"
478 "Test %%timeit with IPython special syntax"
479 @register_line_magic
479 @register_line_magic
480 def lmagic(line):
480 def lmagic(line):
481 ip = get_ipython()
481 ip = get_ipython()
482 ip.user_ns['lmagic_out'] = line
482 ip.user_ns['lmagic_out'] = line
483
483
484 # line mode test
484 # line mode test
485 _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line')
485 _ip.run_line_magic('timeit', '-n1 -r1 %lmagic my line')
486 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
486 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
487 # cell mode test
487 # cell mode test
488 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
488 _ip.run_cell_magic('timeit', '-n1 -r1', '%lmagic my line2')
489 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
489 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
490
490
491 def test_timeit_return():
492 """
493 test wether timeit -o return object
494 """
495
496 res = _ip.run_line_magic('timeit','-n10 -r10 -o 1')
497 assert(res is not None)
498
499 def test_timeit_quiet():
500 """
501 test quiet option of timeit magic
502 """
503 with tt.AssertNotPrints("loops"):
504 _ip.run_cell("%timeit -n1 -r1 -q 1")
491
505
492 @dec.skipif(execution.profile is None)
506 @dec.skipif(execution.profile is None)
493 def test_prun_special_syntax():
507 def test_prun_special_syntax():
494 "Test %%prun with IPython special syntax"
508 "Test %%prun with IPython special syntax"
495 @register_line_magic
509 @register_line_magic
496 def lmagic(line):
510 def lmagic(line):
497 ip = get_ipython()
511 ip = get_ipython()
498 ip.user_ns['lmagic_out'] = line
512 ip.user_ns['lmagic_out'] = line
499
513
500 # line mode test
514 # line mode test
501 _ip.run_line_magic('prun', '-q %lmagic my line')
515 _ip.run_line_magic('prun', '-q %lmagic my line')
502 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
516 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line')
503 # cell mode test
517 # cell mode test
504 _ip.run_cell_magic('prun', '-q', '%lmagic my line2')
518 _ip.run_cell_magic('prun', '-q', '%lmagic my line2')
505 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
519 nt.assert_equal(_ip.user_ns['lmagic_out'], 'my line2')
506
520
507 @dec.skipif(execution.profile is None)
521 @dec.skipif(execution.profile is None)
508 def test_prun_quotes():
522 def test_prun_quotes():
509 "Test that prun does not clobber string escapes (GH #1302)"
523 "Test that prun does not clobber string escapes (GH #1302)"
510 _ip.magic(r"prun -q x = '\t'")
524 _ip.magic(r"prun -q x = '\t'")
511 nt.assert_equal(_ip.user_ns['x'], '\t')
525 nt.assert_equal(_ip.user_ns['x'], '\t')
512
526
513 def test_extension():
527 def test_extension():
514 tmpdir = TemporaryDirectory()
528 tmpdir = TemporaryDirectory()
515 orig_ipython_dir = _ip.ipython_dir
529 orig_ipython_dir = _ip.ipython_dir
516 try:
530 try:
517 _ip.ipython_dir = tmpdir.name
531 _ip.ipython_dir = tmpdir.name
518 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
532 nt.assert_raises(ImportError, _ip.magic, "load_ext daft_extension")
519 url = os.path.join(os.path.dirname(__file__), "daft_extension.py")
533 url = os.path.join(os.path.dirname(__file__), "daft_extension.py")
520 _ip.magic("install_ext %s" % url)
534 _ip.magic("install_ext %s" % url)
521 _ip.user_ns.pop('arq', None)
535 _ip.user_ns.pop('arq', None)
522 invalidate_caches() # Clear import caches
536 invalidate_caches() # Clear import caches
523 _ip.magic("load_ext daft_extension")
537 _ip.magic("load_ext daft_extension")
524 nt.assert_equal(_ip.user_ns['arq'], 185)
538 nt.assert_equal(_ip.user_ns['arq'], 185)
525 _ip.magic("unload_ext daft_extension")
539 _ip.magic("unload_ext daft_extension")
526 assert 'arq' not in _ip.user_ns
540 assert 'arq' not in _ip.user_ns
527 finally:
541 finally:
528 _ip.ipython_dir = orig_ipython_dir
542 _ip.ipython_dir = orig_ipython_dir
529 tmpdir.cleanup()
543 tmpdir.cleanup()
530
544
531 def test_notebook_export_json():
545 def test_notebook_export_json():
532 with TemporaryDirectory() as td:
546 with TemporaryDirectory() as td:
533 outfile = os.path.join(td, "nb.ipynb")
547 outfile = os.path.join(td, "nb.ipynb")
534 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
548 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
535 _ip.magic("notebook -e %s" % outfile)
549 _ip.magic("notebook -e %s" % outfile)
536
550
537 def test_notebook_export_py():
551 def test_notebook_export_py():
538 with TemporaryDirectory() as td:
552 with TemporaryDirectory() as td:
539 outfile = os.path.join(td, "nb.py")
553 outfile = os.path.join(td, "nb.py")
540 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
554 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
541 _ip.magic("notebook -e %s" % outfile)
555 _ip.magic("notebook -e %s" % outfile)
542
556
543 def test_notebook_reformat_py():
557 def test_notebook_reformat_py():
544 with TemporaryDirectory() as td:
558 with TemporaryDirectory() as td:
545 infile = os.path.join(td, "nb.ipynb")
559 infile = os.path.join(td, "nb.ipynb")
546 with io.open(infile, 'w', encoding='utf-8') as f:
560 with io.open(infile, 'w', encoding='utf-8') as f:
547 current.write(nb0, f, 'json')
561 current.write(nb0, f, 'json')
548
562
549 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
563 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
550 _ip.magic("notebook -f py %s" % infile)
564 _ip.magic("notebook -f py %s" % infile)
551
565
552 def test_notebook_reformat_json():
566 def test_notebook_reformat_json():
553 with TemporaryDirectory() as td:
567 with TemporaryDirectory() as td:
554 infile = os.path.join(td, "nb.py")
568 infile = os.path.join(td, "nb.py")
555 with io.open(infile, 'w', encoding='utf-8') as f:
569 with io.open(infile, 'w', encoding='utf-8') as f:
556 current.write(nb0, f, 'py')
570 current.write(nb0, f, 'py')
557
571
558 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
572 _ip.ex(py3compat.u_format(u"u = {u}'hΓ©llo'"))
559 _ip.magic("notebook -f ipynb %s" % infile)
573 _ip.magic("notebook -f ipynb %s" % infile)
560 _ip.magic("notebook -f json %s" % infile)
574 _ip.magic("notebook -f json %s" % infile)
561
575
562 def test_env():
576 def test_env():
563 env = _ip.magic("env")
577 env = _ip.magic("env")
564 assert isinstance(env, dict), type(env)
578 assert isinstance(env, dict), type(env)
565
579
566
580
567 class CellMagicTestCase(TestCase):
581 class CellMagicTestCase(TestCase):
568
582
569 def check_ident(self, magic):
583 def check_ident(self, magic):
570 # Manually called, we get the result
584 # Manually called, we get the result
571 out = _ip.run_cell_magic(magic, 'a', 'b')
585 out = _ip.run_cell_magic(magic, 'a', 'b')
572 nt.assert_equal(out, ('a','b'))
586 nt.assert_equal(out, ('a','b'))
573 # Via run_cell, it goes into the user's namespace via displayhook
587 # Via run_cell, it goes into the user's namespace via displayhook
574 _ip.run_cell('%%' + magic +' c\nd')
588 _ip.run_cell('%%' + magic +' c\nd')
575 nt.assert_equal(_ip.user_ns['_'], ('c','d'))
589 nt.assert_equal(_ip.user_ns['_'], ('c','d'))
576
590
577 def test_cell_magic_func_deco(self):
591 def test_cell_magic_func_deco(self):
578 "Cell magic using simple decorator"
592 "Cell magic using simple decorator"
579 @register_cell_magic
593 @register_cell_magic
580 def cellm(line, cell):
594 def cellm(line, cell):
581 return line, cell
595 return line, cell
582
596
583 self.check_ident('cellm')
597 self.check_ident('cellm')
584
598
585 def test_cell_magic_reg(self):
599 def test_cell_magic_reg(self):
586 "Cell magic manually registered"
600 "Cell magic manually registered"
587 def cellm(line, cell):
601 def cellm(line, cell):
588 return line, cell
602 return line, cell
589
603
590 _ip.register_magic_function(cellm, 'cell', 'cellm2')
604 _ip.register_magic_function(cellm, 'cell', 'cellm2')
591 self.check_ident('cellm2')
605 self.check_ident('cellm2')
592
606
593 def test_cell_magic_class(self):
607 def test_cell_magic_class(self):
594 "Cell magics declared via a class"
608 "Cell magics declared via a class"
595 @magics_class
609 @magics_class
596 class MyMagics(Magics):
610 class MyMagics(Magics):
597
611
598 @cell_magic
612 @cell_magic
599 def cellm3(self, line, cell):
613 def cellm3(self, line, cell):
600 return line, cell
614 return line, cell
601
615
602 _ip.register_magics(MyMagics)
616 _ip.register_magics(MyMagics)
603 self.check_ident('cellm3')
617 self.check_ident('cellm3')
604
618
605 def test_cell_magic_class2(self):
619 def test_cell_magic_class2(self):
606 "Cell magics declared via a class, #2"
620 "Cell magics declared via a class, #2"
607 @magics_class
621 @magics_class
608 class MyMagics2(Magics):
622 class MyMagics2(Magics):
609
623
610 @cell_magic('cellm4')
624 @cell_magic('cellm4')
611 def cellm33(self, line, cell):
625 def cellm33(self, line, cell):
612 return line, cell
626 return line, cell
613
627
614 _ip.register_magics(MyMagics2)
628 _ip.register_magics(MyMagics2)
615 self.check_ident('cellm4')
629 self.check_ident('cellm4')
616 # Check that nothing is registered as 'cellm33'
630 # Check that nothing is registered as 'cellm33'
617 c33 = _ip.find_cell_magic('cellm33')
631 c33 = _ip.find_cell_magic('cellm33')
618 nt.assert_equal(c33, None)
632 nt.assert_equal(c33, None)
619
633
620 def test_file():
634 def test_file():
621 """Basic %%file"""
635 """Basic %%file"""
622 ip = get_ipython()
636 ip = get_ipython()
623 with TemporaryDirectory() as td:
637 with TemporaryDirectory() as td:
624 fname = os.path.join(td, 'file1')
638 fname = os.path.join(td, 'file1')
625 ip.run_cell_magic("file", fname, u'\n'.join([
639 ip.run_cell_magic("file", fname, u'\n'.join([
626 'line1',
640 'line1',
627 'line2',
641 'line2',
628 ]))
642 ]))
629 with open(fname) as f:
643 with open(fname) as f:
630 s = f.read()
644 s = f.read()
631 nt.assert_in('line1\n', s)
645 nt.assert_in('line1\n', s)
632 nt.assert_in('line2', s)
646 nt.assert_in('line2', s)
633
647
634 def test_file_var_expand():
648 def test_file_var_expand():
635 """%%file $filename"""
649 """%%file $filename"""
636 ip = get_ipython()
650 ip = get_ipython()
637 with TemporaryDirectory() as td:
651 with TemporaryDirectory() as td:
638 fname = os.path.join(td, 'file1')
652 fname = os.path.join(td, 'file1')
639 ip.user_ns['filename'] = fname
653 ip.user_ns['filename'] = fname
640 ip.run_cell_magic("file", '$filename', u'\n'.join([
654 ip.run_cell_magic("file", '$filename', u'\n'.join([
641 'line1',
655 'line1',
642 'line2',
656 'line2',
643 ]))
657 ]))
644 with open(fname) as f:
658 with open(fname) as f:
645 s = f.read()
659 s = f.read()
646 nt.assert_in('line1\n', s)
660 nt.assert_in('line1\n', s)
647 nt.assert_in('line2', s)
661 nt.assert_in('line2', s)
648
662
649 def test_file_unicode():
663 def test_file_unicode():
650 """%%file with unicode cell"""
664 """%%file with unicode cell"""
651 ip = get_ipython()
665 ip = get_ipython()
652 with TemporaryDirectory() as td:
666 with TemporaryDirectory() as td:
653 fname = os.path.join(td, 'file1')
667 fname = os.path.join(td, 'file1')
654 ip.run_cell_magic("file", fname, u'\n'.join([
668 ip.run_cell_magic("file", fname, u'\n'.join([
655 u'linΓ©1',
669 u'linΓ©1',
656 u'linΓ©2',
670 u'linΓ©2',
657 ]))
671 ]))
658 with io.open(fname, encoding='utf-8') as f:
672 with io.open(fname, encoding='utf-8') as f:
659 s = f.read()
673 s = f.read()
660 nt.assert_in(u'linΓ©1\n', s)
674 nt.assert_in(u'linΓ©1\n', s)
661 nt.assert_in(u'linΓ©2', s)
675 nt.assert_in(u'linΓ©2', s)
662
676
663 def test_file_amend():
677 def test_file_amend():
664 """%%file -a amends files"""
678 """%%file -a amends files"""
665 ip = get_ipython()
679 ip = get_ipython()
666 with TemporaryDirectory() as td:
680 with TemporaryDirectory() as td:
667 fname = os.path.join(td, 'file2')
681 fname = os.path.join(td, 'file2')
668 ip.run_cell_magic("file", fname, u'\n'.join([
682 ip.run_cell_magic("file", fname, u'\n'.join([
669 'line1',
683 'line1',
670 'line2',
684 'line2',
671 ]))
685 ]))
672 ip.run_cell_magic("file", "-a %s" % fname, u'\n'.join([
686 ip.run_cell_magic("file", "-a %s" % fname, u'\n'.join([
673 'line3',
687 'line3',
674 'line4',
688 'line4',
675 ]))
689 ]))
676 with open(fname) as f:
690 with open(fname) as f:
677 s = f.read()
691 s = f.read()
678 nt.assert_in('line1\n', s)
692 nt.assert_in('line1\n', s)
679 nt.assert_in('line3\n', s)
693 nt.assert_in('line3\n', s)
680
694
681
695
682 def test_script_config():
696 def test_script_config():
683 ip = get_ipython()
697 ip = get_ipython()
684 ip.config.ScriptMagics.script_magics = ['whoda']
698 ip.config.ScriptMagics.script_magics = ['whoda']
685 sm = script.ScriptMagics(shell=ip)
699 sm = script.ScriptMagics(shell=ip)
686 nt.assert_in('whoda', sm.magics['cell'])
700 nt.assert_in('whoda', sm.magics['cell'])
687
701
688 @dec.skip_win32
702 @dec.skip_win32
689 def test_script_out():
703 def test_script_out():
690 ip = get_ipython()
704 ip = get_ipython()
691 ip.run_cell_magic("script", "--out output sh", "echo 'hi'")
705 ip.run_cell_magic("script", "--out output sh", "echo 'hi'")
692 nt.assert_equal(ip.user_ns['output'], 'hi\n')
706 nt.assert_equal(ip.user_ns['output'], 'hi\n')
693
707
694 @dec.skip_win32
708 @dec.skip_win32
695 def test_script_err():
709 def test_script_err():
696 ip = get_ipython()
710 ip = get_ipython()
697 ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2")
711 ip.run_cell_magic("script", "--err error sh", "echo 'hello' >&2")
698 nt.assert_equal(ip.user_ns['error'], 'hello\n')
712 nt.assert_equal(ip.user_ns['error'], 'hello\n')
699
713
700 @dec.skip_win32
714 @dec.skip_win32
701 def test_script_out_err():
715 def test_script_out_err():
702 ip = get_ipython()
716 ip = get_ipython()
703 ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2")
717 ip.run_cell_magic("script", "--out output --err error sh", "echo 'hi'\necho 'hello' >&2")
704 nt.assert_equal(ip.user_ns['output'], 'hi\n')
718 nt.assert_equal(ip.user_ns['output'], 'hi\n')
705 nt.assert_equal(ip.user_ns['error'], 'hello\n')
719 nt.assert_equal(ip.user_ns['error'], 'hello\n')
706
720
707 @dec.skip_win32
721 @dec.skip_win32
708 def test_script_bg_out():
722 def test_script_bg_out():
709 ip = get_ipython()
723 ip = get_ipython()
710 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
724 ip.run_cell_magic("script", "--bg --out output sh", "echo 'hi'")
711 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
725 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
712
726
713 @dec.skip_win32
727 @dec.skip_win32
714 def test_script_bg_err():
728 def test_script_bg_err():
715 ip = get_ipython()
729 ip = get_ipython()
716 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
730 ip.run_cell_magic("script", "--bg --err error sh", "echo 'hello' >&2")
717 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
731 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
718
732
719 @dec.skip_win32
733 @dec.skip_win32
720 def test_script_bg_out_err():
734 def test_script_bg_out_err():
721 ip = get_ipython()
735 ip = get_ipython()
722 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
736 ip.run_cell_magic("script", "--bg --out output --err error sh", "echo 'hi'\necho 'hello' >&2")
723 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
737 nt.assert_equal(ip.user_ns['output'].read(), b'hi\n')
724 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
738 nt.assert_equal(ip.user_ns['error'].read(), b'hello\n')
725
739
726 def test_script_defaults():
740 def test_script_defaults():
727 ip = get_ipython()
741 ip = get_ipython()
728 for cmd in ['sh', 'bash', 'perl', 'ruby']:
742 for cmd in ['sh', 'bash', 'perl', 'ruby']:
729 try:
743 try:
730 find_cmd(cmd)
744 find_cmd(cmd)
731 except Exception:
745 except Exception:
732 pass
746 pass
733 else:
747 else:
734 nt.assert_in(cmd, ip.magics_manager.magics['cell'])
748 nt.assert_in(cmd, ip.magics_manager.magics['cell'])
735
749
736
750
737 @magics_class
751 @magics_class
738 class FooFoo(Magics):
752 class FooFoo(Magics):
739 """class with both %foo and %%foo magics"""
753 """class with both %foo and %%foo magics"""
740 @line_magic('foo')
754 @line_magic('foo')
741 def line_foo(self, line):
755 def line_foo(self, line):
742 "I am line foo"
756 "I am line foo"
743 pass
757 pass
744
758
745 @cell_magic("foo")
759 @cell_magic("foo")
746 def cell_foo(self, line, cell):
760 def cell_foo(self, line, cell):
747 "I am cell foo, not line foo"
761 "I am cell foo, not line foo"
748 pass
762 pass
749
763
750 def test_line_cell_info():
764 def test_line_cell_info():
751 """%%foo and %foo magics are distinguishable to inspect"""
765 """%%foo and %foo magics are distinguishable to inspect"""
752 ip = get_ipython()
766 ip = get_ipython()
753 ip.magics_manager.register(FooFoo)
767 ip.magics_manager.register(FooFoo)
754 oinfo = ip.object_inspect('foo')
768 oinfo = ip.object_inspect('foo')
755 nt.assert_true(oinfo['found'])
769 nt.assert_true(oinfo['found'])
756 nt.assert_true(oinfo['ismagic'])
770 nt.assert_true(oinfo['ismagic'])
757
771
758 oinfo = ip.object_inspect('%%foo')
772 oinfo = ip.object_inspect('%%foo')
759 nt.assert_true(oinfo['found'])
773 nt.assert_true(oinfo['found'])
760 nt.assert_true(oinfo['ismagic'])
774 nt.assert_true(oinfo['ismagic'])
761 nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__)
775 nt.assert_equal(oinfo['docstring'], FooFoo.cell_foo.__doc__)
762
776
763 oinfo = ip.object_inspect('%foo')
777 oinfo = ip.object_inspect('%foo')
764 nt.assert_true(oinfo['found'])
778 nt.assert_true(oinfo['found'])
765 nt.assert_true(oinfo['ismagic'])
779 nt.assert_true(oinfo['ismagic'])
766 nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__)
780 nt.assert_equal(oinfo['docstring'], FooFoo.line_foo.__doc__)
767
781
768 def test_multiple_magics():
782 def test_multiple_magics():
769 ip = get_ipython()
783 ip = get_ipython()
770 foo1 = FooFoo(ip)
784 foo1 = FooFoo(ip)
771 foo2 = FooFoo(ip)
785 foo2 = FooFoo(ip)
772 mm = ip.magics_manager
786 mm = ip.magics_manager
773 mm.register(foo1)
787 mm.register(foo1)
774 nt.assert_true(mm.magics['line']['foo'].im_self is foo1)
788 nt.assert_true(mm.magics['line']['foo'].im_self is foo1)
775 mm.register(foo2)
789 mm.register(foo2)
776 nt.assert_true(mm.magics['line']['foo'].im_self is foo2)
790 nt.assert_true(mm.magics['line']['foo'].im_self is foo2)
777
791
778 def test_alias_magic():
792 def test_alias_magic():
779 """Test %alias_magic."""
793 """Test %alias_magic."""
780 ip = get_ipython()
794 ip = get_ipython()
781 mm = ip.magics_manager
795 mm = ip.magics_manager
782
796
783 # Basic operation: both cell and line magics are created, if possible.
797 # Basic operation: both cell and line magics are created, if possible.
784 ip.run_line_magic('alias_magic', 'timeit_alias timeit')
798 ip.run_line_magic('alias_magic', 'timeit_alias timeit')
785 nt.assert_in('timeit_alias', mm.magics['line'])
799 nt.assert_in('timeit_alias', mm.magics['line'])
786 nt.assert_in('timeit_alias', mm.magics['cell'])
800 nt.assert_in('timeit_alias', mm.magics['cell'])
787
801
788 # --cell is specified, line magic not created.
802 # --cell is specified, line magic not created.
789 ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit')
803 ip.run_line_magic('alias_magic', '--cell timeit_cell_alias timeit')
790 nt.assert_not_in('timeit_cell_alias', mm.magics['line'])
804 nt.assert_not_in('timeit_cell_alias', mm.magics['line'])
791 nt.assert_in('timeit_cell_alias', mm.magics['cell'])
805 nt.assert_in('timeit_cell_alias', mm.magics['cell'])
792
806
793 # Test that line alias is created successfully.
807 # Test that line alias is created successfully.
794 ip.run_line_magic('alias_magic', '--line env_alias env')
808 ip.run_line_magic('alias_magic', '--line env_alias env')
795 nt.assert_equal(ip.run_line_magic('env', ''),
809 nt.assert_equal(ip.run_line_magic('env', ''),
796 ip.run_line_magic('env_alias', ''))
810 ip.run_line_magic('env_alias', ''))
797
811
798 def test_save():
812 def test_save():
799 """Test %save."""
813 """Test %save."""
800 ip = get_ipython()
814 ip = get_ipython()
801 ip.history_manager.reset() # Clear any existing history.
815 ip.history_manager.reset() # Clear any existing history.
802 cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"]
816 cmds = [u"a=1", u"def b():\n return a**2", u"print(a, b())"]
803 for i, cmd in enumerate(cmds, start=1):
817 for i, cmd in enumerate(cmds, start=1):
804 ip.history_manager.store_inputs(i, cmd)
818 ip.history_manager.store_inputs(i, cmd)
805 with TemporaryDirectory() as tmpdir:
819 with TemporaryDirectory() as tmpdir:
806 file = os.path.join(tmpdir, "testsave.py")
820 file = os.path.join(tmpdir, "testsave.py")
807 ip.run_line_magic("save", "%s 1-10" % file)
821 ip.run_line_magic("save", "%s 1-10" % file)
808 with open(file) as f:
822 with open(file) as f:
809 content = f.read()
823 content = f.read()
810 nt.assert_equal(content.count(cmds[0]), 1)
824 nt.assert_equal(content.count(cmds[0]), 1)
811 nt.assert_in('coding: utf-8', content)
825 nt.assert_in('coding: utf-8', content)
812 ip.run_line_magic("save", "-a %s 1-10" % file)
826 ip.run_line_magic("save", "-a %s 1-10" % file)
813 with open(file) as f:
827 with open(file) as f:
814 content = f.read()
828 content = f.read()
815 nt.assert_equal(content.count(cmds[0]), 2)
829 nt.assert_equal(content.count(cmds[0]), 2)
816 nt.assert_in('coding: utf-8', content)
830 nt.assert_in('coding: utf-8', content)
817
831
818
832
819 def test_store():
833 def test_store():
820 """Test %store."""
834 """Test %store."""
821 ip = get_ipython()
835 ip = get_ipython()
822 ip.run_line_magic('load_ext', 'storemagic')
836 ip.run_line_magic('load_ext', 'storemagic')
823
837
824 # make sure the storage is empty
838 # make sure the storage is empty
825 ip.run_line_magic('store', '-z')
839 ip.run_line_magic('store', '-z')
826 ip.user_ns['var'] = 42
840 ip.user_ns['var'] = 42
827 ip.run_line_magic('store', 'var')
841 ip.run_line_magic('store', 'var')
828 ip.user_ns['var'] = 39
842 ip.user_ns['var'] = 39
829 ip.run_line_magic('store', '-r')
843 ip.run_line_magic('store', '-r')
830 nt.assert_equal(ip.user_ns['var'], 42)
844 nt.assert_equal(ip.user_ns['var'], 42)
831
845
832 ip.run_line_magic('store', '-d var')
846 ip.run_line_magic('store', '-d var')
833 ip.user_ns['var'] = 39
847 ip.user_ns['var'] = 39
834 ip.run_line_magic('store' , '-r')
848 ip.run_line_magic('store' , '-r')
835 nt.assert_equal(ip.user_ns['var'], 39)
849 nt.assert_equal(ip.user_ns['var'], 39)
836
850
837
851
838 def _run_edit_test(arg_s, exp_filename=None,
852 def _run_edit_test(arg_s, exp_filename=None,
839 exp_lineno=-1,
853 exp_lineno=-1,
840 exp_contents=None,
854 exp_contents=None,
841 exp_is_temp=None):
855 exp_is_temp=None):
842 ip = get_ipython()
856 ip = get_ipython()
843 M = code.CodeMagics(ip)
857 M = code.CodeMagics(ip)
844 last_call = ['','']
858 last_call = ['','']
845 opts,args = M.parse_options(arg_s,'prxn:')
859 opts,args = M.parse_options(arg_s,'prxn:')
846 filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call)
860 filename, lineno, is_temp = M._find_edit_target(ip, args, opts, last_call)
847
861
848 if exp_filename is not None:
862 if exp_filename is not None:
849 nt.assert_equal(exp_filename, filename)
863 nt.assert_equal(exp_filename, filename)
850 if exp_contents is not None:
864 if exp_contents is not None:
851 with io.open(filename, 'r') as f:
865 with io.open(filename, 'r') as f:
852 contents = f.read()
866 contents = f.read()
853 nt.assert_equal(exp_contents, contents)
867 nt.assert_equal(exp_contents, contents)
854 if exp_lineno != -1:
868 if exp_lineno != -1:
855 nt.assert_equal(exp_lineno, lineno)
869 nt.assert_equal(exp_lineno, lineno)
856 if exp_is_temp is not None:
870 if exp_is_temp is not None:
857 nt.assert_equal(exp_is_temp, is_temp)
871 nt.assert_equal(exp_is_temp, is_temp)
858
872
859
873
860 def test_edit_interactive():
874 def test_edit_interactive():
861 """%edit on interactively defined objects"""
875 """%edit on interactively defined objects"""
862 ip = get_ipython()
876 ip = get_ipython()
863 n = ip.execution_count
877 n = ip.execution_count
864 ip.run_cell(u"def foo(): return 1", store_history=True)
878 ip.run_cell(u"def foo(): return 1", store_history=True)
865
879
866 try:
880 try:
867 _run_edit_test("foo")
881 _run_edit_test("foo")
868 except code.InteractivelyDefined as e:
882 except code.InteractivelyDefined as e:
869 nt.assert_equal(e.index, n)
883 nt.assert_equal(e.index, n)
870 else:
884 else:
871 raise AssertionError("Should have raised InteractivelyDefined")
885 raise AssertionError("Should have raised InteractivelyDefined")
872
886
873
887
874 def test_edit_cell():
888 def test_edit_cell():
875 """%edit [cell id]"""
889 """%edit [cell id]"""
876 ip = get_ipython()
890 ip = get_ipython()
877
891
878 ip.run_cell(u"def foo(): return 1", store_history=True)
892 ip.run_cell(u"def foo(): return 1", store_history=True)
879
893
880 # test
894 # test
881 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
895 _run_edit_test("1", exp_contents=ip.user_ns['In'][1], exp_is_temp=True)
General Comments 0
You need to be logged in to leave comments. Login now