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