##// END OF EJS Templates
Merge pull request #12301 from datadesk/run-magic-raise-errors
Matthias Bussonnier -
r25792:c24e8af4 merge
parent child Browse files
Show More
@@ -1,1492 +1,1490 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Implementation of execution-related magic functions."""
2 """Implementation of execution-related magic functions."""
3
3
4 # 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
7
8 import ast
8 import ast
9 import bdb
9 import bdb
10 import builtins as builtin_mod
10 import builtins as builtin_mod
11 import gc
11 import gc
12 import itertools
12 import itertools
13 import os
13 import os
14 import shlex
14 import shlex
15 import sys
15 import sys
16 import time
16 import time
17 import timeit
17 import timeit
18 import math
18 import math
19 import re
19 import re
20 from pdb import Restart
20 from pdb import Restart
21
21
22 import cProfile as profile
22 import cProfile as profile
23 import pstats
23 import pstats
24
24
25 from IPython.core import oinspect
25 from IPython.core import oinspect
26 from IPython.core import magic_arguments
26 from IPython.core import magic_arguments
27 from IPython.core import page
27 from IPython.core import page
28 from IPython.core.error import UsageError
28 from IPython.core.error import UsageError
29 from IPython.core.macro import Macro
29 from IPython.core.macro import Macro
30 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
30 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
31 line_cell_magic, on_off, needs_local_scope,
31 line_cell_magic, on_off, needs_local_scope,
32 no_var_expand)
32 no_var_expand)
33 from IPython.testing.skipdoctest import skip_doctest
33 from IPython.testing.skipdoctest import skip_doctest
34 from IPython.utils.contexts import preserve_keys
34 from IPython.utils.contexts import preserve_keys
35 from IPython.utils.capture import capture_output
35 from IPython.utils.capture import capture_output
36 from IPython.utils.ipstruct import Struct
36 from IPython.utils.ipstruct import Struct
37 from IPython.utils.module_paths import find_mod
37 from IPython.utils.module_paths import find_mod
38 from IPython.utils.path import get_py_filename, shellglob
38 from IPython.utils.path import get_py_filename, shellglob
39 from IPython.utils.timing import clock, clock2
39 from IPython.utils.timing import clock, clock2
40 from warnings import warn
40 from warnings import warn
41 from logging import error
41 from logging import error
42 from io import StringIO
42 from io import StringIO
43
43
44 if sys.version_info > (3,8):
44 if sys.version_info > (3,8):
45 from ast import Module
45 from ast import Module
46 else :
46 else :
47 # mock the new API, ignore second argument
47 # mock the new API, ignore second argument
48 # see https://github.com/ipython/ipython/issues/11590
48 # see https://github.com/ipython/ipython/issues/11590
49 from ast import Module as OriginalModule
49 from ast import Module as OriginalModule
50 Module = lambda nodelist, type_ignores: OriginalModule(nodelist)
50 Module = lambda nodelist, type_ignores: OriginalModule(nodelist)
51
51
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 Contains the following attributes :
62 Contains the following attributes :
63
63
64 loops: (int) number of loops done per measurement
64 loops: (int) number of loops done per measurement
65 repeat: (int) number of times the measurement has been repeated
65 repeat: (int) number of times the measurement has been repeated
66 best: (float) best execution time / number
66 best: (float) best execution time / number
67 all_runs: (list of float) execution time of each run (in s)
67 all_runs: (list of float) execution 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 def __init__(self, loops, repeat, best, worst, all_runs, compile_time, precision):
71 def __init__(self, loops, repeat, best, worst, all_runs, compile_time, precision):
72 self.loops = loops
72 self.loops = loops
73 self.repeat = repeat
73 self.repeat = repeat
74 self.best = best
74 self.best = best
75 self.worst = worst
75 self.worst = worst
76 self.all_runs = all_runs
76 self.all_runs = all_runs
77 self.compile_time = compile_time
77 self.compile_time = compile_time
78 self._precision = precision
78 self._precision = precision
79 self.timings = [ dt / self.loops for dt in all_runs]
79 self.timings = [ dt / self.loops for dt in all_runs]
80
80
81 @property
81 @property
82 def average(self):
82 def average(self):
83 return math.fsum(self.timings) / len(self.timings)
83 return math.fsum(self.timings) / len(self.timings)
84
84
85 @property
85 @property
86 def stdev(self):
86 def stdev(self):
87 mean = self.average
87 mean = self.average
88 return (math.fsum([(x - mean) ** 2 for x in self.timings]) / len(self.timings)) ** 0.5
88 return (math.fsum([(x - mean) ** 2 for x in self.timings]) / len(self.timings)) ** 0.5
89
89
90 def __str__(self):
90 def __str__(self):
91 pm = '+-'
91 pm = '+-'
92 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
92 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
93 try:
93 try:
94 u'\xb1'.encode(sys.stdout.encoding)
94 u'\xb1'.encode(sys.stdout.encoding)
95 pm = u'\xb1'
95 pm = u'\xb1'
96 except:
96 except:
97 pass
97 pass
98 return (
98 return (
99 u"{mean} {pm} {std} per loop (mean {pm} std. dev. of {runs} run{run_plural}, {loops} loop{loop_plural} each)"
99 u"{mean} {pm} {std} per loop (mean {pm} std. dev. of {runs} run{run_plural}, {loops} loop{loop_plural} each)"
100 .format(
100 .format(
101 pm = pm,
101 pm = pm,
102 runs = self.repeat,
102 runs = self.repeat,
103 loops = self.loops,
103 loops = self.loops,
104 loop_plural = "" if self.loops == 1 else "s",
104 loop_plural = "" if self.loops == 1 else "s",
105 run_plural = "" if self.repeat == 1 else "s",
105 run_plural = "" if self.repeat == 1 else "s",
106 mean = _format_time(self.average, self._precision),
106 mean = _format_time(self.average, self._precision),
107 std = _format_time(self.stdev, self._precision))
107 std = _format_time(self.stdev, self._precision))
108 )
108 )
109
109
110 def _repr_pretty_(self, p , cycle):
110 def _repr_pretty_(self, p , cycle):
111 unic = self.__str__()
111 unic = self.__str__()
112 p.text(u'<TimeitResult : '+unic+u'>')
112 p.text(u'<TimeitResult : '+unic+u'>')
113
113
114
114
115 class TimeitTemplateFiller(ast.NodeTransformer):
115 class TimeitTemplateFiller(ast.NodeTransformer):
116 """Fill in the AST template for timing execution.
116 """Fill in the AST template for timing execution.
117
117
118 This is quite closely tied to the template definition, which is in
118 This is quite closely tied to the template definition, which is in
119 :meth:`ExecutionMagics.timeit`.
119 :meth:`ExecutionMagics.timeit`.
120 """
120 """
121 def __init__(self, ast_setup, ast_stmt):
121 def __init__(self, ast_setup, ast_stmt):
122 self.ast_setup = ast_setup
122 self.ast_setup = ast_setup
123 self.ast_stmt = ast_stmt
123 self.ast_stmt = ast_stmt
124
124
125 def visit_FunctionDef(self, node):
125 def visit_FunctionDef(self, node):
126 "Fill in the setup statement"
126 "Fill in the setup statement"
127 self.generic_visit(node)
127 self.generic_visit(node)
128 if node.name == "inner":
128 if node.name == "inner":
129 node.body[:1] = self.ast_setup.body
129 node.body[:1] = self.ast_setup.body
130
130
131 return node
131 return node
132
132
133 def visit_For(self, node):
133 def visit_For(self, node):
134 "Fill in the statement to be timed"
134 "Fill in the statement to be timed"
135 if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt':
135 if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt':
136 node.body = self.ast_stmt.body
136 node.body = self.ast_stmt.body
137 return node
137 return node
138
138
139
139
140 class Timer(timeit.Timer):
140 class Timer(timeit.Timer):
141 """Timer class that explicitly uses self.inner
141 """Timer class that explicitly uses self.inner
142
142
143 which is an undocumented implementation detail of CPython,
143 which is an undocumented implementation detail of CPython,
144 not shared by PyPy.
144 not shared by PyPy.
145 """
145 """
146 # Timer.timeit copied from CPython 3.4.2
146 # Timer.timeit copied from CPython 3.4.2
147 def timeit(self, number=timeit.default_number):
147 def timeit(self, number=timeit.default_number):
148 """Time 'number' executions of the main statement.
148 """Time 'number' executions of the main statement.
149
149
150 To be precise, this executes the setup statement once, and
150 To be precise, this executes the setup statement once, and
151 then returns the time it takes to execute the main statement
151 then returns the time it takes to execute the main statement
152 a number of times, as a float measured in seconds. The
152 a number of times, as a float measured in seconds. The
153 argument is the number of times through the loop, defaulting
153 argument is the number of times through the loop, defaulting
154 to one million. The main statement, the setup statement and
154 to one million. The main statement, the setup statement and
155 the timer function to be used are passed to the constructor.
155 the timer function to be used are passed to the constructor.
156 """
156 """
157 it = itertools.repeat(None, number)
157 it = itertools.repeat(None, number)
158 gcold = gc.isenabled()
158 gcold = gc.isenabled()
159 gc.disable()
159 gc.disable()
160 try:
160 try:
161 timing = self.inner(it, self.timer)
161 timing = self.inner(it, self.timer)
162 finally:
162 finally:
163 if gcold:
163 if gcold:
164 gc.enable()
164 gc.enable()
165 return timing
165 return timing
166
166
167
167
168 @magics_class
168 @magics_class
169 class ExecutionMagics(Magics):
169 class ExecutionMagics(Magics):
170 """Magics related to code execution, debugging, profiling, etc.
170 """Magics related to code execution, debugging, profiling, etc.
171
171
172 """
172 """
173
173
174 def __init__(self, shell):
174 def __init__(self, shell):
175 super(ExecutionMagics, self).__init__(shell)
175 super(ExecutionMagics, self).__init__(shell)
176 # Default execution function used to actually run user code.
176 # Default execution function used to actually run user code.
177 self.default_runner = None
177 self.default_runner = None
178
178
179 @skip_doctest
179 @skip_doctest
180 @no_var_expand
180 @no_var_expand
181 @line_cell_magic
181 @line_cell_magic
182 def prun(self, parameter_s='', cell=None):
182 def prun(self, parameter_s='', cell=None):
183
183
184 """Run a statement through the python code profiler.
184 """Run a statement through the python code profiler.
185
185
186 Usage, in line mode:
186 Usage, in line mode:
187 %prun [options] statement
187 %prun [options] statement
188
188
189 Usage, in cell mode:
189 Usage, in cell mode:
190 %%prun [options] [statement]
190 %%prun [options] [statement]
191 code...
191 code...
192 code...
192 code...
193
193
194 In cell mode, the additional code lines are appended to the (possibly
194 In cell mode, the additional code lines are appended to the (possibly
195 empty) statement in the first line. Cell mode allows you to easily
195 empty) statement in the first line. Cell mode allows you to easily
196 profile multiline blocks without having to put them in a separate
196 profile multiline blocks without having to put them in a separate
197 function.
197 function.
198
198
199 The given statement (which doesn't require quote marks) is run via the
199 The given statement (which doesn't require quote marks) is run via the
200 python profiler in a manner similar to the profile.run() function.
200 python profiler in a manner similar to the profile.run() function.
201 Namespaces are internally managed to work correctly; profile.run
201 Namespaces are internally managed to work correctly; profile.run
202 cannot be used in IPython because it makes certain assumptions about
202 cannot be used in IPython because it makes certain assumptions about
203 namespaces which do not hold under IPython.
203 namespaces which do not hold under IPython.
204
204
205 Options:
205 Options:
206
206
207 -l <limit>
207 -l <limit>
208 you can place restrictions on what or how much of the
208 you can place restrictions on what or how much of the
209 profile gets printed. The limit value can be:
209 profile gets printed. The limit value can be:
210
210
211 * A string: only information for function names containing this string
211 * A string: only information for function names containing this string
212 is printed.
212 is printed.
213
213
214 * An integer: only these many lines are printed.
214 * An integer: only these many lines are printed.
215
215
216 * A float (between 0 and 1): this fraction of the report is printed
216 * A float (between 0 and 1): this fraction of the report is printed
217 (for example, use a limit of 0.4 to see the topmost 40% only).
217 (for example, use a limit of 0.4 to see the topmost 40% only).
218
218
219 You can combine several limits with repeated use of the option. For
219 You can combine several limits with repeated use of the option. For
220 example, ``-l __init__ -l 5`` will print only the topmost 5 lines of
220 example, ``-l __init__ -l 5`` will print only the topmost 5 lines of
221 information about class constructors.
221 information about class constructors.
222
222
223 -r
223 -r
224 return the pstats.Stats object generated by the profiling. This
224 return the pstats.Stats object generated by the profiling. This
225 object has all the information about the profile in it, and you can
225 object has all the information about the profile in it, and you can
226 later use it for further analysis or in other functions.
226 later use it for further analysis or in other functions.
227
227
228 -s <key>
228 -s <key>
229 sort profile by given key. You can provide more than one key
229 sort profile by given key. You can provide more than one key
230 by using the option several times: '-s key1 -s key2 -s key3...'. The
230 by using the option several times: '-s key1 -s key2 -s key3...'. The
231 default sorting key is 'time'.
231 default sorting key is 'time'.
232
232
233 The following is copied verbatim from the profile documentation
233 The following is copied verbatim from the profile documentation
234 referenced below:
234 referenced below:
235
235
236 When more than one key is provided, additional keys are used as
236 When more than one key is provided, additional keys are used as
237 secondary criteria when the there is equality in all keys selected
237 secondary criteria when the there is equality in all keys selected
238 before them.
238 before them.
239
239
240 Abbreviations can be used for any key names, as long as the
240 Abbreviations can be used for any key names, as long as the
241 abbreviation is unambiguous. The following are the keys currently
241 abbreviation is unambiguous. The following are the keys currently
242 defined:
242 defined:
243
243
244 ============ =====================
244 ============ =====================
245 Valid Arg Meaning
245 Valid Arg Meaning
246 ============ =====================
246 ============ =====================
247 "calls" call count
247 "calls" call count
248 "cumulative" cumulative time
248 "cumulative" cumulative time
249 "file" file name
249 "file" file name
250 "module" file name
250 "module" file name
251 "pcalls" primitive call count
251 "pcalls" primitive call count
252 "line" line number
252 "line" line number
253 "name" function name
253 "name" function name
254 "nfl" name/file/line
254 "nfl" name/file/line
255 "stdname" standard name
255 "stdname" standard name
256 "time" internal time
256 "time" internal time
257 ============ =====================
257 ============ =====================
258
258
259 Note that all sorts on statistics are in descending order (placing
259 Note that all sorts on statistics are in descending order (placing
260 most time consuming items first), where as name, file, and line number
260 most time consuming items first), where as name, file, and line number
261 searches are in ascending order (i.e., alphabetical). The subtle
261 searches are in ascending order (i.e., alphabetical). The subtle
262 distinction between "nfl" and "stdname" is that the standard name is a
262 distinction between "nfl" and "stdname" is that the standard name is a
263 sort of the name as printed, which means that the embedded line
263 sort of the name as printed, which means that the embedded line
264 numbers get compared in an odd way. For example, lines 3, 20, and 40
264 numbers get compared in an odd way. For example, lines 3, 20, and 40
265 would (if the file names were the same) appear in the string order
265 would (if the file names were the same) appear in the string order
266 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
266 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
267 line numbers. In fact, sort_stats("nfl") is the same as
267 line numbers. In fact, sort_stats("nfl") is the same as
268 sort_stats("name", "file", "line").
268 sort_stats("name", "file", "line").
269
269
270 -T <filename>
270 -T <filename>
271 save profile results as shown on screen to a text
271 save profile results as shown on screen to a text
272 file. The profile is still shown on screen.
272 file. The profile is still shown on screen.
273
273
274 -D <filename>
274 -D <filename>
275 save (via dump_stats) profile statistics to given
275 save (via dump_stats) profile statistics to given
276 filename. This data is in a format understood by the pstats module, and
276 filename. This data is in a format understood by the pstats module, and
277 is generated by a call to the dump_stats() method of profile
277 is generated by a call to the dump_stats() method of profile
278 objects. The profile is still shown on screen.
278 objects. The profile is still shown on screen.
279
279
280 -q
280 -q
281 suppress output to the pager. Best used with -T and/or -D above.
281 suppress output to the pager. Best used with -T and/or -D above.
282
282
283 If you want to run complete programs under the profiler's control, use
283 If you want to run complete programs under the profiler's control, use
284 ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts
284 ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts
285 contains profiler specific options as described here.
285 contains profiler specific options as described here.
286
286
287 You can read the complete documentation for the profile module with::
287 You can read the complete documentation for the profile module with::
288
288
289 In [1]: import profile; profile.help()
289 In [1]: import profile; profile.help()
290
290
291 .. versionchanged:: 7.3
291 .. versionchanged:: 7.3
292 User variables are no longer expanded,
292 User variables are no longer expanded,
293 the magic line is always left unmodified.
293 the magic line is always left unmodified.
294
294
295 """
295 """
296 opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q',
296 opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q',
297 list_all=True, posix=False)
297 list_all=True, posix=False)
298 if cell is not None:
298 if cell is not None:
299 arg_str += '\n' + cell
299 arg_str += '\n' + cell
300 arg_str = self.shell.transform_cell(arg_str)
300 arg_str = self.shell.transform_cell(arg_str)
301 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
301 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
302
302
303 def _run_with_profiler(self, code, opts, namespace):
303 def _run_with_profiler(self, code, opts, namespace):
304 """
304 """
305 Run `code` with profiler. Used by ``%prun`` and ``%run -p``.
305 Run `code` with profiler. Used by ``%prun`` and ``%run -p``.
306
306
307 Parameters
307 Parameters
308 ----------
308 ----------
309 code : str
309 code : str
310 Code to be executed.
310 Code to be executed.
311 opts : Struct
311 opts : Struct
312 Options parsed by `self.parse_options`.
312 Options parsed by `self.parse_options`.
313 namespace : dict
313 namespace : dict
314 A dictionary for Python namespace (e.g., `self.shell.user_ns`).
314 A dictionary for Python namespace (e.g., `self.shell.user_ns`).
315
315
316 """
316 """
317
317
318 # Fill default values for unspecified options:
318 # Fill default values for unspecified options:
319 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
319 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
320
320
321 prof = profile.Profile()
321 prof = profile.Profile()
322 try:
322 try:
323 prof = prof.runctx(code, namespace, namespace)
323 prof = prof.runctx(code, namespace, namespace)
324 sys_exit = ''
324 sys_exit = ''
325 except SystemExit:
325 except SystemExit:
326 sys_exit = """*** SystemExit exception caught in code being profiled."""
326 sys_exit = """*** SystemExit exception caught in code being profiled."""
327
327
328 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
328 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
329
329
330 lims = opts.l
330 lims = opts.l
331 if lims:
331 if lims:
332 lims = [] # rebuild lims with ints/floats/strings
332 lims = [] # rebuild lims with ints/floats/strings
333 for lim in opts.l:
333 for lim in opts.l:
334 try:
334 try:
335 lims.append(int(lim))
335 lims.append(int(lim))
336 except ValueError:
336 except ValueError:
337 try:
337 try:
338 lims.append(float(lim))
338 lims.append(float(lim))
339 except ValueError:
339 except ValueError:
340 lims.append(lim)
340 lims.append(lim)
341
341
342 # Trap output.
342 # Trap output.
343 stdout_trap = StringIO()
343 stdout_trap = StringIO()
344 stats_stream = stats.stream
344 stats_stream = stats.stream
345 try:
345 try:
346 stats.stream = stdout_trap
346 stats.stream = stdout_trap
347 stats.print_stats(*lims)
347 stats.print_stats(*lims)
348 finally:
348 finally:
349 stats.stream = stats_stream
349 stats.stream = stats_stream
350
350
351 output = stdout_trap.getvalue()
351 output = stdout_trap.getvalue()
352 output = output.rstrip()
352 output = output.rstrip()
353
353
354 if 'q' not in opts:
354 if 'q' not in opts:
355 page.page(output)
355 page.page(output)
356 print(sys_exit, end=' ')
356 print(sys_exit, end=' ')
357
357
358 dump_file = opts.D[0]
358 dump_file = opts.D[0]
359 text_file = opts.T[0]
359 text_file = opts.T[0]
360 if dump_file:
360 if dump_file:
361 prof.dump_stats(dump_file)
361 prof.dump_stats(dump_file)
362 print('\n*** Profile stats marshalled to file',\
362 print('\n*** Profile stats marshalled to file',\
363 repr(dump_file)+'.',sys_exit)
363 repr(dump_file)+'.',sys_exit)
364 if text_file:
364 if text_file:
365 with open(text_file, 'w') as pfile:
365 with open(text_file, 'w') as pfile:
366 pfile.write(output)
366 pfile.write(output)
367 print('\n*** Profile printout saved to text file',\
367 print('\n*** Profile printout saved to text file',\
368 repr(text_file)+'.',sys_exit)
368 repr(text_file)+'.',sys_exit)
369
369
370 if 'r' in opts:
370 if 'r' in opts:
371 return stats
371 return stats
372 else:
372 else:
373 return None
373 return None
374
374
375 @line_magic
375 @line_magic
376 def pdb(self, parameter_s=''):
376 def pdb(self, parameter_s=''):
377 """Control the automatic calling of the pdb interactive debugger.
377 """Control the automatic calling of the pdb interactive debugger.
378
378
379 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
379 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
380 argument it works as a toggle.
380 argument it works as a toggle.
381
381
382 When an exception is triggered, IPython can optionally call the
382 When an exception is triggered, IPython can optionally call the
383 interactive pdb debugger after the traceback printout. %pdb toggles
383 interactive pdb debugger after the traceback printout. %pdb toggles
384 this feature on and off.
384 this feature on and off.
385
385
386 The initial state of this feature is set in your configuration
386 The initial state of this feature is set in your configuration
387 file (the option is ``InteractiveShell.pdb``).
387 file (the option is ``InteractiveShell.pdb``).
388
388
389 If you want to just activate the debugger AFTER an exception has fired,
389 If you want to just activate the debugger AFTER an exception has fired,
390 without having to type '%pdb on' and rerunning your code, you can use
390 without having to type '%pdb on' and rerunning your code, you can use
391 the %debug magic."""
391 the %debug magic."""
392
392
393 par = parameter_s.strip().lower()
393 par = parameter_s.strip().lower()
394
394
395 if par:
395 if par:
396 try:
396 try:
397 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
397 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
398 except KeyError:
398 except KeyError:
399 print ('Incorrect argument. Use on/1, off/0, '
399 print ('Incorrect argument. Use on/1, off/0, '
400 'or nothing for a toggle.')
400 'or nothing for a toggle.')
401 return
401 return
402 else:
402 else:
403 # toggle
403 # toggle
404 new_pdb = not self.shell.call_pdb
404 new_pdb = not self.shell.call_pdb
405
405
406 # set on the shell
406 # set on the shell
407 self.shell.call_pdb = new_pdb
407 self.shell.call_pdb = new_pdb
408 print('Automatic pdb calling has been turned',on_off(new_pdb))
408 print('Automatic pdb calling has been turned',on_off(new_pdb))
409
409
410 @skip_doctest
410 @skip_doctest
411 @magic_arguments.magic_arguments()
411 @magic_arguments.magic_arguments()
412 @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE',
412 @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE',
413 help="""
413 help="""
414 Set break point at LINE in FILE.
414 Set break point at LINE in FILE.
415 """
415 """
416 )
416 )
417 @magic_arguments.argument('statement', nargs='*',
417 @magic_arguments.argument('statement', nargs='*',
418 help="""
418 help="""
419 Code to run in debugger.
419 Code to run in debugger.
420 You can omit this in cell magic mode.
420 You can omit this in cell magic mode.
421 """
421 """
422 )
422 )
423 @no_var_expand
423 @no_var_expand
424 @line_cell_magic
424 @line_cell_magic
425 def debug(self, line='', cell=None):
425 def debug(self, line='', cell=None):
426 """Activate the interactive debugger.
426 """Activate the interactive debugger.
427
427
428 This magic command support two ways of activating debugger.
428 This magic command support two ways of activating debugger.
429 One is to activate debugger before executing code. This way, you
429 One is to activate debugger before executing code. This way, you
430 can set a break point, to step through the code from the point.
430 can set a break point, to step through the code from the point.
431 You can use this mode by giving statements to execute and optionally
431 You can use this mode by giving statements to execute and optionally
432 a breakpoint.
432 a breakpoint.
433
433
434 The other one is to activate debugger in post-mortem mode. You can
434 The other one is to activate debugger in post-mortem mode. You can
435 activate this mode simply running %debug without any argument.
435 activate this mode simply running %debug without any argument.
436 If an exception has just occurred, this lets you inspect its stack
436 If an exception has just occurred, this lets you inspect its stack
437 frames interactively. Note that this will always work only on the last
437 frames interactively. Note that this will always work only on the last
438 traceback that occurred, so you must call this quickly after an
438 traceback that occurred, so you must call this quickly after an
439 exception that you wish to inspect has fired, because if another one
439 exception that you wish to inspect has fired, because if another one
440 occurs, it clobbers the previous one.
440 occurs, it clobbers the previous one.
441
441
442 If you want IPython to automatically do this on every exception, see
442 If you want IPython to automatically do this on every exception, see
443 the %pdb magic for more details.
443 the %pdb magic for more details.
444
444
445 .. versionchanged:: 7.3
445 .. versionchanged:: 7.3
446 When running code, user variables are no longer expanded,
446 When running code, user variables are no longer expanded,
447 the magic line is always left unmodified.
447 the magic line is always left unmodified.
448
448
449 """
449 """
450 args = magic_arguments.parse_argstring(self.debug, line)
450 args = magic_arguments.parse_argstring(self.debug, line)
451
451
452 if not (args.breakpoint or args.statement or cell):
452 if not (args.breakpoint or args.statement or cell):
453 self._debug_post_mortem()
453 self._debug_post_mortem()
454 else:
454 else:
455 code = "\n".join(args.statement)
455 code = "\n".join(args.statement)
456 if cell:
456 if cell:
457 code += "\n" + cell
457 code += "\n" + cell
458 self._debug_exec(code, args.breakpoint)
458 self._debug_exec(code, args.breakpoint)
459
459
460 def _debug_post_mortem(self):
460 def _debug_post_mortem(self):
461 self.shell.debugger(force=True)
461 self.shell.debugger(force=True)
462
462
463 def _debug_exec(self, code, breakpoint):
463 def _debug_exec(self, code, breakpoint):
464 if breakpoint:
464 if breakpoint:
465 (filename, bp_line) = breakpoint.rsplit(':', 1)
465 (filename, bp_line) = breakpoint.rsplit(':', 1)
466 bp_line = int(bp_line)
466 bp_line = int(bp_line)
467 else:
467 else:
468 (filename, bp_line) = (None, None)
468 (filename, bp_line) = (None, None)
469 self._run_with_debugger(code, self.shell.user_ns, filename, bp_line)
469 self._run_with_debugger(code, self.shell.user_ns, filename, bp_line)
470
470
471 @line_magic
471 @line_magic
472 def tb(self, s):
472 def tb(self, s):
473 """Print the last traceback.
473 """Print the last traceback.
474
474
475 Optionally, specify an exception reporting mode, tuning the
475 Optionally, specify an exception reporting mode, tuning the
476 verbosity of the traceback. By default the currently-active exception
476 verbosity of the traceback. By default the currently-active exception
477 mode is used. See %xmode for changing exception reporting modes.
477 mode is used. See %xmode for changing exception reporting modes.
478
478
479 Valid modes: Plain, Context, Verbose, and Minimal.
479 Valid modes: Plain, Context, Verbose, and Minimal.
480 """
480 """
481 interactive_tb = self.shell.InteractiveTB
481 interactive_tb = self.shell.InteractiveTB
482 if s:
482 if s:
483 # Switch exception reporting mode for this one call.
483 # Switch exception reporting mode for this one call.
484 # Ensure it is switched back.
484 # Ensure it is switched back.
485 def xmode_switch_err(name):
485 def xmode_switch_err(name):
486 warn('Error changing %s exception modes.\n%s' %
486 warn('Error changing %s exception modes.\n%s' %
487 (name,sys.exc_info()[1]))
487 (name,sys.exc_info()[1]))
488
488
489 new_mode = s.strip().capitalize()
489 new_mode = s.strip().capitalize()
490 original_mode = interactive_tb.mode
490 original_mode = interactive_tb.mode
491 try:
491 try:
492 try:
492 try:
493 interactive_tb.set_mode(mode=new_mode)
493 interactive_tb.set_mode(mode=new_mode)
494 except Exception:
494 except Exception:
495 xmode_switch_err('user')
495 xmode_switch_err('user')
496 else:
496 else:
497 self.shell.showtraceback()
497 self.shell.showtraceback()
498 finally:
498 finally:
499 interactive_tb.set_mode(mode=original_mode)
499 interactive_tb.set_mode(mode=original_mode)
500 else:
500 else:
501 self.shell.showtraceback()
501 self.shell.showtraceback()
502
502
503 @skip_doctest
503 @skip_doctest
504 @line_magic
504 @line_magic
505 def run(self, parameter_s='', runner=None,
505 def run(self, parameter_s='', runner=None,
506 file_finder=get_py_filename):
506 file_finder=get_py_filename):
507 """Run the named file inside IPython as a program.
507 """Run the named file inside IPython as a program.
508
508
509 Usage::
509 Usage::
510
510
511 %run [-n -i -e -G]
511 %run [-n -i -e -G]
512 [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
512 [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
513 ( -m mod | file ) [args]
513 ( -m mod | file ) [args]
514
514
515 Parameters after the filename are passed as command-line arguments to
515 Parameters after the filename are passed as command-line arguments to
516 the program (put in sys.argv). Then, control returns to IPython's
516 the program (put in sys.argv). Then, control returns to IPython's
517 prompt.
517 prompt.
518
518
519 This is similar to running at a system prompt ``python file args``,
519 This is similar to running at a system prompt ``python file args``,
520 but with the advantage of giving you IPython's tracebacks, and of
520 but with the advantage of giving you IPython's tracebacks, and of
521 loading all variables into your interactive namespace for further use
521 loading all variables into your interactive namespace for further use
522 (unless -p is used, see below).
522 (unless -p is used, see below).
523
523
524 The file is executed in a namespace initially consisting only of
524 The file is executed in a namespace initially consisting only of
525 ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus
525 ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus
526 sees its environment as if it were being run as a stand-alone program
526 sees its environment as if it were being run as a stand-alone program
527 (except for sharing global objects such as previously imported
527 (except for sharing global objects such as previously imported
528 modules). But after execution, the IPython interactive namespace gets
528 modules). But after execution, the IPython interactive namespace gets
529 updated with all variables defined in the program (except for __name__
529 updated with all variables defined in the program (except for __name__
530 and sys.argv). This allows for very convenient loading of code for
530 and sys.argv). This allows for very convenient loading of code for
531 interactive work, while giving each program a 'clean sheet' to run in.
531 interactive work, while giving each program a 'clean sheet' to run in.
532
532
533 Arguments are expanded using shell-like glob match. Patterns
533 Arguments are expanded using shell-like glob match. Patterns
534 '*', '?', '[seq]' and '[!seq]' can be used. Additionally,
534 '*', '?', '[seq]' and '[!seq]' can be used. Additionally,
535 tilde '~' will be expanded into user's home directory. Unlike
535 tilde '~' will be expanded into user's home directory. Unlike
536 real shells, quotation does not suppress expansions. Use
536 real shells, quotation does not suppress expansions. Use
537 *two* back slashes (e.g. ``\\\\*``) to suppress expansions.
537 *two* back slashes (e.g. ``\\\\*``) to suppress expansions.
538 To completely disable these expansions, you can use -G flag.
538 To completely disable these expansions, you can use -G flag.
539
539
540 On Windows systems, the use of single quotes `'` when specifying
540 On Windows systems, the use of single quotes `'` when specifying
541 a file is not supported. Use double quotes `"`.
541 a file is not supported. Use double quotes `"`.
542
542
543 Options:
543 Options:
544
544
545 -n
545 -n
546 __name__ is NOT set to '__main__', but to the running file's name
546 __name__ is NOT set to '__main__', but to the running file's name
547 without extension (as python does under import). This allows running
547 without extension (as python does under import). This allows running
548 scripts and reloading the definitions in them without calling code
548 scripts and reloading the definitions in them without calling code
549 protected by an ``if __name__ == "__main__"`` clause.
549 protected by an ``if __name__ == "__main__"`` clause.
550
550
551 -i
551 -i
552 run the file in IPython's namespace instead of an empty one. This
552 run the file in IPython's namespace instead of an empty one. This
553 is useful if you are experimenting with code written in a text editor
553 is useful if you are experimenting with code written in a text editor
554 which depends on variables defined interactively.
554 which depends on variables defined interactively.
555
555
556 -e
556 -e
557 ignore sys.exit() calls or SystemExit exceptions in the script
557 ignore sys.exit() calls or SystemExit exceptions in the script
558 being run. This is particularly useful if IPython is being used to
558 being run. This is particularly useful if IPython is being used to
559 run unittests, which always exit with a sys.exit() call. In such
559 run unittests, which always exit with a sys.exit() call. In such
560 cases you are interested in the output of the test results, not in
560 cases you are interested in the output of the test results, not in
561 seeing a traceback of the unittest module.
561 seeing a traceback of the unittest module.
562
562
563 -t
563 -t
564 print timing information at the end of the run. IPython will give
564 print timing information at the end of the run. IPython will give
565 you an estimated CPU time consumption for your script, which under
565 you an estimated CPU time consumption for your script, which under
566 Unix uses the resource module to avoid the wraparound problems of
566 Unix uses the resource module to avoid the wraparound problems of
567 time.clock(). Under Unix, an estimate of time spent on system tasks
567 time.clock(). Under Unix, an estimate of time spent on system tasks
568 is also given (for Windows platforms this is reported as 0.0).
568 is also given (for Windows platforms this is reported as 0.0).
569
569
570 If -t is given, an additional ``-N<N>`` option can be given, where <N>
570 If -t is given, an additional ``-N<N>`` option can be given, where <N>
571 must be an integer indicating how many times you want the script to
571 must be an integer indicating how many times you want the script to
572 run. The final timing report will include total and per run results.
572 run. The final timing report will include total and per run results.
573
573
574 For example (testing the script uniq_stable.py)::
574 For example (testing the script uniq_stable.py)::
575
575
576 In [1]: run -t uniq_stable
576 In [1]: run -t uniq_stable
577
577
578 IPython CPU timings (estimated):
578 IPython CPU timings (estimated):
579 User : 0.19597 s.
579 User : 0.19597 s.
580 System: 0.0 s.
580 System: 0.0 s.
581
581
582 In [2]: run -t -N5 uniq_stable
582 In [2]: run -t -N5 uniq_stable
583
583
584 IPython CPU timings (estimated):
584 IPython CPU timings (estimated):
585 Total runs performed: 5
585 Total runs performed: 5
586 Times : Total Per run
586 Times : Total Per run
587 User : 0.910862 s, 0.1821724 s.
587 User : 0.910862 s, 0.1821724 s.
588 System: 0.0 s, 0.0 s.
588 System: 0.0 s, 0.0 s.
589
589
590 -d
590 -d
591 run your program under the control of pdb, the Python debugger.
591 run your program under the control of pdb, the Python debugger.
592 This allows you to execute your program step by step, watch variables,
592 This allows you to execute your program step by step, watch variables,
593 etc. Internally, what IPython does is similar to calling::
593 etc. Internally, what IPython does is similar to calling::
594
594
595 pdb.run('execfile("YOURFILENAME")')
595 pdb.run('execfile("YOURFILENAME")')
596
596
597 with a breakpoint set on line 1 of your file. You can change the line
597 with a breakpoint set on line 1 of your file. You can change the line
598 number for this automatic breakpoint to be <N> by using the -bN option
598 number for this automatic breakpoint to be <N> by using the -bN option
599 (where N must be an integer). For example::
599 (where N must be an integer). For example::
600
600
601 %run -d -b40 myscript
601 %run -d -b40 myscript
602
602
603 will set the first breakpoint at line 40 in myscript.py. Note that
603 will set the first breakpoint at line 40 in myscript.py. Note that
604 the first breakpoint must be set on a line which actually does
604 the first breakpoint must be set on a line which actually does
605 something (not a comment or docstring) for it to stop execution.
605 something (not a comment or docstring) for it to stop execution.
606
606
607 Or you can specify a breakpoint in a different file::
607 Or you can specify a breakpoint in a different file::
608
608
609 %run -d -b myotherfile.py:20 myscript
609 %run -d -b myotherfile.py:20 myscript
610
610
611 When the pdb debugger starts, you will see a (Pdb) prompt. You must
611 When the pdb debugger starts, you will see a (Pdb) prompt. You must
612 first enter 'c' (without quotes) to start execution up to the first
612 first enter 'c' (without quotes) to start execution up to the first
613 breakpoint.
613 breakpoint.
614
614
615 Entering 'help' gives information about the use of the debugger. You
615 Entering 'help' gives information about the use of the debugger. You
616 can easily see pdb's full documentation with "import pdb;pdb.help()"
616 can easily see pdb's full documentation with "import pdb;pdb.help()"
617 at a prompt.
617 at a prompt.
618
618
619 -p
619 -p
620 run program under the control of the Python profiler module (which
620 run program under the control of the Python profiler module (which
621 prints a detailed report of execution times, function calls, etc).
621 prints a detailed report of execution times, function calls, etc).
622
622
623 You can pass other options after -p which affect the behavior of the
623 You can pass other options after -p which affect the behavior of the
624 profiler itself. See the docs for %prun for details.
624 profiler itself. See the docs for %prun for details.
625
625
626 In this mode, the program's variables do NOT propagate back to the
626 In this mode, the program's variables do NOT propagate back to the
627 IPython interactive namespace (because they remain in the namespace
627 IPython interactive namespace (because they remain in the namespace
628 where the profiler executes them).
628 where the profiler executes them).
629
629
630 Internally this triggers a call to %prun, see its documentation for
630 Internally this triggers a call to %prun, see its documentation for
631 details on the options available specifically for profiling.
631 details on the options available specifically for profiling.
632
632
633 There is one special usage for which the text above doesn't apply:
633 There is one special usage for which the text above doesn't apply:
634 if the filename ends with .ipy[nb], the file is run as ipython script,
634 if the filename ends with .ipy[nb], the file is run as ipython script,
635 just as if the commands were written on IPython prompt.
635 just as if the commands were written on IPython prompt.
636
636
637 -m
637 -m
638 specify module name to load instead of script path. Similar to
638 specify module name to load instead of script path. Similar to
639 the -m option for the python interpreter. Use this option last if you
639 the -m option for the python interpreter. Use this option last if you
640 want to combine with other %run options. Unlike the python interpreter
640 want to combine with other %run options. Unlike the python interpreter
641 only source modules are allowed no .pyc or .pyo files.
641 only source modules are allowed no .pyc or .pyo files.
642 For example::
642 For example::
643
643
644 %run -m example
644 %run -m example
645
645
646 will run the example module.
646 will run the example module.
647
647
648 -G
648 -G
649 disable shell-like glob expansion of arguments.
649 disable shell-like glob expansion of arguments.
650
650
651 """
651 """
652
652
653 # Logic to handle issue #3664
653 # Logic to handle issue #3664
654 # Add '--' after '-m <module_name>' to ignore additional args passed to a module.
654 # Add '--' after '-m <module_name>' to ignore additional args passed to a module.
655 if '-m' in parameter_s and '--' not in parameter_s:
655 if '-m' in parameter_s and '--' not in parameter_s:
656 argv = shlex.split(parameter_s, posix=(os.name == 'posix'))
656 argv = shlex.split(parameter_s, posix=(os.name == 'posix'))
657 for idx, arg in enumerate(argv):
657 for idx, arg in enumerate(argv):
658 if arg and arg.startswith('-') and arg != '-':
658 if arg and arg.startswith('-') and arg != '-':
659 if arg == '-m':
659 if arg == '-m':
660 argv.insert(idx + 2, '--')
660 argv.insert(idx + 2, '--')
661 break
661 break
662 else:
662 else:
663 # Positional arg, break
663 # Positional arg, break
664 break
664 break
665 parameter_s = ' '.join(shlex.quote(arg) for arg in argv)
665 parameter_s = ' '.join(shlex.quote(arg) for arg in argv)
666
666
667 # get arguments and set sys.argv for program to be run.
667 # get arguments and set sys.argv for program to be run.
668 opts, arg_lst = self.parse_options(parameter_s,
668 opts, arg_lst = self.parse_options(parameter_s,
669 'nidtN:b:pD:l:rs:T:em:G',
669 'nidtN:b:pD:l:rs:T:em:G',
670 mode='list', list_all=1)
670 mode='list', list_all=1)
671 if "m" in opts:
671 if "m" in opts:
672 modulename = opts["m"][0]
672 modulename = opts["m"][0]
673 modpath = find_mod(modulename)
673 modpath = find_mod(modulename)
674 if modpath is None:
674 if modpath is None:
675 warn('%r is not a valid modulename on sys.path'%modulename)
675 msg = '%r is not a valid modulename on sys.path'%modulename
676 return
676 raise Exception(msg)
677 arg_lst = [modpath] + arg_lst
677 arg_lst = [modpath] + arg_lst
678 try:
678 try:
679 fpath = None # initialize to make sure fpath is in scope later
679 fpath = None # initialize to make sure fpath is in scope later
680 fpath = arg_lst[0]
680 fpath = arg_lst[0]
681 filename = file_finder(fpath)
681 filename = file_finder(fpath)
682 except IndexError:
682 except IndexError:
683 warn('you must provide at least a filename.')
683 msg = 'you must provide at least a filename.'
684 print('\n%run:\n', oinspect.getdoc(self.run))
684 raise Exception(msg)
685 return
686 except IOError as e:
685 except IOError as e:
687 try:
686 try:
688 msg = str(e)
687 msg = str(e)
689 except UnicodeError:
688 except UnicodeError:
690 msg = e.message
689 msg = e.message
691 if os.name == 'nt' and re.match(r"^'.*'$",fpath):
690 if os.name == 'nt' and re.match(r"^'.*'$",fpath):
692 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
691 warn('For Windows, use double quotes to wrap a filename: %run "mypath\\myfile.py"')
693 error(msg)
692 raise Exception(msg)
694 return
695 except TypeError:
693 except TypeError:
696 if fpath in sys.meta_path:
694 if fpath in sys.meta_path:
697 filename = ""
695 filename = ""
698 else:
696 else:
699 raise
697 raise
700
698
701 if filename.lower().endswith(('.ipy', '.ipynb')):
699 if filename.lower().endswith(('.ipy', '.ipynb')):
702 with preserve_keys(self.shell.user_ns, '__file__'):
700 with preserve_keys(self.shell.user_ns, '__file__'):
703 self.shell.user_ns['__file__'] = filename
701 self.shell.user_ns['__file__'] = filename
704 self.shell.safe_execfile_ipy(filename)
702 self.shell.safe_execfile_ipy(filename, raise_exceptions=True)
705 return
703 return
706
704
707 # Control the response to exit() calls made by the script being run
705 # Control the response to exit() calls made by the script being run
708 exit_ignore = 'e' in opts
706 exit_ignore = 'e' in opts
709
707
710 # Make sure that the running script gets a proper sys.argv as if it
708 # Make sure that the running script gets a proper sys.argv as if it
711 # were run from a system shell.
709 # were run from a system shell.
712 save_argv = sys.argv # save it for later restoring
710 save_argv = sys.argv # save it for later restoring
713
711
714 if 'G' in opts:
712 if 'G' in opts:
715 args = arg_lst[1:]
713 args = arg_lst[1:]
716 else:
714 else:
717 # tilde and glob expansion
715 # tilde and glob expansion
718 args = shellglob(map(os.path.expanduser, arg_lst[1:]))
716 args = shellglob(map(os.path.expanduser, arg_lst[1:]))
719
717
720 sys.argv = [filename] + args # put in the proper filename
718 sys.argv = [filename] + args # put in the proper filename
721
719
722 if 'n' in opts:
720 if 'n' in opts:
723 name = os.path.splitext(os.path.basename(filename))[0]
721 name = os.path.splitext(os.path.basename(filename))[0]
724 else:
722 else:
725 name = '__main__'
723 name = '__main__'
726
724
727 if 'i' in opts:
725 if 'i' in opts:
728 # Run in user's interactive namespace
726 # Run in user's interactive namespace
729 prog_ns = self.shell.user_ns
727 prog_ns = self.shell.user_ns
730 __name__save = self.shell.user_ns['__name__']
728 __name__save = self.shell.user_ns['__name__']
731 prog_ns['__name__'] = name
729 prog_ns['__name__'] = name
732 main_mod = self.shell.user_module
730 main_mod = self.shell.user_module
733
731
734 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
732 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
735 # set the __file__ global in the script's namespace
733 # set the __file__ global in the script's namespace
736 # TK: Is this necessary in interactive mode?
734 # TK: Is this necessary in interactive mode?
737 prog_ns['__file__'] = filename
735 prog_ns['__file__'] = filename
738 else:
736 else:
739 # Run in a fresh, empty namespace
737 # Run in a fresh, empty namespace
740
738
741 # The shell MUST hold a reference to prog_ns so after %run
739 # The shell MUST hold a reference to prog_ns so after %run
742 # exits, the python deletion mechanism doesn't zero it out
740 # exits, the python deletion mechanism doesn't zero it out
743 # (leaving dangling references). See interactiveshell for details
741 # (leaving dangling references). See interactiveshell for details
744 main_mod = self.shell.new_main_mod(filename, name)
742 main_mod = self.shell.new_main_mod(filename, name)
745 prog_ns = main_mod.__dict__
743 prog_ns = main_mod.__dict__
746
744
747 # pickle fix. See interactiveshell for an explanation. But we need to
745 # pickle fix. See interactiveshell for an explanation. But we need to
748 # make sure that, if we overwrite __main__, we replace it at the end
746 # make sure that, if we overwrite __main__, we replace it at the end
749 main_mod_name = prog_ns['__name__']
747 main_mod_name = prog_ns['__name__']
750
748
751 if main_mod_name == '__main__':
749 if main_mod_name == '__main__':
752 restore_main = sys.modules['__main__']
750 restore_main = sys.modules['__main__']
753 else:
751 else:
754 restore_main = False
752 restore_main = False
755
753
756 # This needs to be undone at the end to prevent holding references to
754 # This needs to be undone at the end to prevent holding references to
757 # every single object ever created.
755 # every single object ever created.
758 sys.modules[main_mod_name] = main_mod
756 sys.modules[main_mod_name] = main_mod
759
757
760 if 'p' in opts or 'd' in opts:
758 if 'p' in opts or 'd' in opts:
761 if 'm' in opts:
759 if 'm' in opts:
762 code = 'run_module(modulename, prog_ns)'
760 code = 'run_module(modulename, prog_ns)'
763 code_ns = {
761 code_ns = {
764 'run_module': self.shell.safe_run_module,
762 'run_module': self.shell.safe_run_module,
765 'prog_ns': prog_ns,
763 'prog_ns': prog_ns,
766 'modulename': modulename,
764 'modulename': modulename,
767 }
765 }
768 else:
766 else:
769 if 'd' in opts:
767 if 'd' in opts:
770 # allow exceptions to raise in debug mode
768 # allow exceptions to raise in debug mode
771 code = 'execfile(filename, prog_ns, raise_exceptions=True)'
769 code = 'execfile(filename, prog_ns, raise_exceptions=True)'
772 else:
770 else:
773 code = 'execfile(filename, prog_ns)'
771 code = 'execfile(filename, prog_ns)'
774 code_ns = {
772 code_ns = {
775 'execfile': self.shell.safe_execfile,
773 'execfile': self.shell.safe_execfile,
776 'prog_ns': prog_ns,
774 'prog_ns': prog_ns,
777 'filename': get_py_filename(filename),
775 'filename': get_py_filename(filename),
778 }
776 }
779
777
780 try:
778 try:
781 stats = None
779 stats = None
782 if 'p' in opts:
780 if 'p' in opts:
783 stats = self._run_with_profiler(code, opts, code_ns)
781 stats = self._run_with_profiler(code, opts, code_ns)
784 else:
782 else:
785 if 'd' in opts:
783 if 'd' in opts:
786 bp_file, bp_line = parse_breakpoint(
784 bp_file, bp_line = parse_breakpoint(
787 opts.get('b', ['1'])[0], filename)
785 opts.get('b', ['1'])[0], filename)
788 self._run_with_debugger(
786 self._run_with_debugger(
789 code, code_ns, filename, bp_line, bp_file)
787 code, code_ns, filename, bp_line, bp_file)
790 else:
788 else:
791 if 'm' in opts:
789 if 'm' in opts:
792 def run():
790 def run():
793 self.shell.safe_run_module(modulename, prog_ns)
791 self.shell.safe_run_module(modulename, prog_ns)
794 else:
792 else:
795 if runner is None:
793 if runner is None:
796 runner = self.default_runner
794 runner = self.default_runner
797 if runner is None:
795 if runner is None:
798 runner = self.shell.safe_execfile
796 runner = self.shell.safe_execfile
799
797
800 def run():
798 def run():
801 runner(filename, prog_ns, prog_ns,
799 runner(filename, prog_ns, prog_ns,
802 exit_ignore=exit_ignore)
800 exit_ignore=exit_ignore)
803
801
804 if 't' in opts:
802 if 't' in opts:
805 # timed execution
803 # timed execution
806 try:
804 try:
807 nruns = int(opts['N'][0])
805 nruns = int(opts['N'][0])
808 if nruns < 1:
806 if nruns < 1:
809 error('Number of runs must be >=1')
807 error('Number of runs must be >=1')
810 return
808 return
811 except (KeyError):
809 except (KeyError):
812 nruns = 1
810 nruns = 1
813 self._run_with_timing(run, nruns)
811 self._run_with_timing(run, nruns)
814 else:
812 else:
815 # regular execution
813 # regular execution
816 run()
814 run()
817
815
818 if 'i' in opts:
816 if 'i' in opts:
819 self.shell.user_ns['__name__'] = __name__save
817 self.shell.user_ns['__name__'] = __name__save
820 else:
818 else:
821 # update IPython interactive namespace
819 # update IPython interactive namespace
822
820
823 # Some forms of read errors on the file may mean the
821 # Some forms of read errors on the file may mean the
824 # __name__ key was never set; using pop we don't have to
822 # __name__ key was never set; using pop we don't have to
825 # worry about a possible KeyError.
823 # worry about a possible KeyError.
826 prog_ns.pop('__name__', None)
824 prog_ns.pop('__name__', None)
827
825
828 with preserve_keys(self.shell.user_ns, '__file__'):
826 with preserve_keys(self.shell.user_ns, '__file__'):
829 self.shell.user_ns.update(prog_ns)
827 self.shell.user_ns.update(prog_ns)
830 finally:
828 finally:
831 # It's a bit of a mystery why, but __builtins__ can change from
829 # It's a bit of a mystery why, but __builtins__ can change from
832 # being a module to becoming a dict missing some key data after
830 # being a module to becoming a dict missing some key data after
833 # %run. As best I can see, this is NOT something IPython is doing
831 # %run. As best I can see, this is NOT something IPython is doing
834 # at all, and similar problems have been reported before:
832 # at all, and similar problems have been reported before:
835 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
833 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
836 # Since this seems to be done by the interpreter itself, the best
834 # Since this seems to be done by the interpreter itself, the best
837 # we can do is to at least restore __builtins__ for the user on
835 # we can do is to at least restore __builtins__ for the user on
838 # exit.
836 # exit.
839 self.shell.user_ns['__builtins__'] = builtin_mod
837 self.shell.user_ns['__builtins__'] = builtin_mod
840
838
841 # Ensure key global structures are restored
839 # Ensure key global structures are restored
842 sys.argv = save_argv
840 sys.argv = save_argv
843 if restore_main:
841 if restore_main:
844 sys.modules['__main__'] = restore_main
842 sys.modules['__main__'] = restore_main
845 if '__mp_main__' in sys.modules:
843 if '__mp_main__' in sys.modules:
846 sys.modules['__mp_main__'] = restore_main
844 sys.modules['__mp_main__'] = restore_main
847 else:
845 else:
848 # Remove from sys.modules the reference to main_mod we'd
846 # Remove from sys.modules the reference to main_mod we'd
849 # added. Otherwise it will trap references to objects
847 # added. Otherwise it will trap references to objects
850 # contained therein.
848 # contained therein.
851 del sys.modules[main_mod_name]
849 del sys.modules[main_mod_name]
852
850
853 return stats
851 return stats
854
852
855 def _run_with_debugger(self, code, code_ns, filename=None,
853 def _run_with_debugger(self, code, code_ns, filename=None,
856 bp_line=None, bp_file=None):
854 bp_line=None, bp_file=None):
857 """
855 """
858 Run `code` in debugger with a break point.
856 Run `code` in debugger with a break point.
859
857
860 Parameters
858 Parameters
861 ----------
859 ----------
862 code : str
860 code : str
863 Code to execute.
861 Code to execute.
864 code_ns : dict
862 code_ns : dict
865 A namespace in which `code` is executed.
863 A namespace in which `code` is executed.
866 filename : str
864 filename : str
867 `code` is ran as if it is in `filename`.
865 `code` is ran as if it is in `filename`.
868 bp_line : int, optional
866 bp_line : int, optional
869 Line number of the break point.
867 Line number of the break point.
870 bp_file : str, optional
868 bp_file : str, optional
871 Path to the file in which break point is specified.
869 Path to the file in which break point is specified.
872 `filename` is used if not given.
870 `filename` is used if not given.
873
871
874 Raises
872 Raises
875 ------
873 ------
876 UsageError
874 UsageError
877 If the break point given by `bp_line` is not valid.
875 If the break point given by `bp_line` is not valid.
878
876
879 """
877 """
880 deb = self.shell.InteractiveTB.pdb
878 deb = self.shell.InteractiveTB.pdb
881 if not deb:
879 if not deb:
882 self.shell.InteractiveTB.pdb = self.shell.InteractiveTB.debugger_cls()
880 self.shell.InteractiveTB.pdb = self.shell.InteractiveTB.debugger_cls()
883 deb = self.shell.InteractiveTB.pdb
881 deb = self.shell.InteractiveTB.pdb
884
882
885 # deb.checkline() fails if deb.curframe exists but is None; it can
883 # deb.checkline() fails if deb.curframe exists but is None; it can
886 # handle it not existing. https://github.com/ipython/ipython/issues/10028
884 # handle it not existing. https://github.com/ipython/ipython/issues/10028
887 if hasattr(deb, 'curframe'):
885 if hasattr(deb, 'curframe'):
888 del deb.curframe
886 del deb.curframe
889
887
890 # reset Breakpoint state, which is moronically kept
888 # reset Breakpoint state, which is moronically kept
891 # in a class
889 # in a class
892 bdb.Breakpoint.next = 1
890 bdb.Breakpoint.next = 1
893 bdb.Breakpoint.bplist = {}
891 bdb.Breakpoint.bplist = {}
894 bdb.Breakpoint.bpbynumber = [None]
892 bdb.Breakpoint.bpbynumber = [None]
895 deb.clear_all_breaks()
893 deb.clear_all_breaks()
896 if bp_line is not None:
894 if bp_line is not None:
897 # Set an initial breakpoint to stop execution
895 # Set an initial breakpoint to stop execution
898 maxtries = 10
896 maxtries = 10
899 bp_file = bp_file or filename
897 bp_file = bp_file or filename
900 checkline = deb.checkline(bp_file, bp_line)
898 checkline = deb.checkline(bp_file, bp_line)
901 if not checkline:
899 if not checkline:
902 for bp in range(bp_line + 1, bp_line + maxtries + 1):
900 for bp in range(bp_line + 1, bp_line + maxtries + 1):
903 if deb.checkline(bp_file, bp):
901 if deb.checkline(bp_file, bp):
904 break
902 break
905 else:
903 else:
906 msg = ("\nI failed to find a valid line to set "
904 msg = ("\nI failed to find a valid line to set "
907 "a breakpoint\n"
905 "a breakpoint\n"
908 "after trying up to line: %s.\n"
906 "after trying up to line: %s.\n"
909 "Please set a valid breakpoint manually "
907 "Please set a valid breakpoint manually "
910 "with the -b option." % bp)
908 "with the -b option." % bp)
911 raise UsageError(msg)
909 raise UsageError(msg)
912 # if we find a good linenumber, set the breakpoint
910 # if we find a good linenumber, set the breakpoint
913 deb.do_break('%s:%s' % (bp_file, bp_line))
911 deb.do_break('%s:%s' % (bp_file, bp_line))
914
912
915 if filename:
913 if filename:
916 # Mimic Pdb._runscript(...)
914 # Mimic Pdb._runscript(...)
917 deb._wait_for_mainpyfile = True
915 deb._wait_for_mainpyfile = True
918 deb.mainpyfile = deb.canonic(filename)
916 deb.mainpyfile = deb.canonic(filename)
919
917
920 # Start file run
918 # Start file run
921 print("NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt)
919 print("NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt)
922 try:
920 try:
923 if filename:
921 if filename:
924 # save filename so it can be used by methods on the deb object
922 # save filename so it can be used by methods on the deb object
925 deb._exec_filename = filename
923 deb._exec_filename = filename
926 while True:
924 while True:
927 try:
925 try:
928 trace = sys.gettrace()
926 trace = sys.gettrace()
929 deb.run(code, code_ns)
927 deb.run(code, code_ns)
930 except Restart:
928 except Restart:
931 print("Restarting")
929 print("Restarting")
932 if filename:
930 if filename:
933 deb._wait_for_mainpyfile = True
931 deb._wait_for_mainpyfile = True
934 deb.mainpyfile = deb.canonic(filename)
932 deb.mainpyfile = deb.canonic(filename)
935 continue
933 continue
936 else:
934 else:
937 break
935 break
938 finally:
936 finally:
939 sys.settrace(trace)
937 sys.settrace(trace)
940
938
941
939
942 except:
940 except:
943 etype, value, tb = sys.exc_info()
941 etype, value, tb = sys.exc_info()
944 # Skip three frames in the traceback: the %run one,
942 # Skip three frames in the traceback: the %run one,
945 # one inside bdb.py, and the command-line typed by the
943 # one inside bdb.py, and the command-line typed by the
946 # user (run by exec in pdb itself).
944 # user (run by exec in pdb itself).
947 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
945 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
948
946
949 @staticmethod
947 @staticmethod
950 def _run_with_timing(run, nruns):
948 def _run_with_timing(run, nruns):
951 """
949 """
952 Run function `run` and print timing information.
950 Run function `run` and print timing information.
953
951
954 Parameters
952 Parameters
955 ----------
953 ----------
956 run : callable
954 run : callable
957 Any callable object which takes no argument.
955 Any callable object which takes no argument.
958 nruns : int
956 nruns : int
959 Number of times to execute `run`.
957 Number of times to execute `run`.
960
958
961 """
959 """
962 twall0 = time.perf_counter()
960 twall0 = time.perf_counter()
963 if nruns == 1:
961 if nruns == 1:
964 t0 = clock2()
962 t0 = clock2()
965 run()
963 run()
966 t1 = clock2()
964 t1 = clock2()
967 t_usr = t1[0] - t0[0]
965 t_usr = t1[0] - t0[0]
968 t_sys = t1[1] - t0[1]
966 t_sys = t1[1] - t0[1]
969 print("\nIPython CPU timings (estimated):")
967 print("\nIPython CPU timings (estimated):")
970 print(" User : %10.2f s." % t_usr)
968 print(" User : %10.2f s." % t_usr)
971 print(" System : %10.2f s." % t_sys)
969 print(" System : %10.2f s." % t_sys)
972 else:
970 else:
973 runs = range(nruns)
971 runs = range(nruns)
974 t0 = clock2()
972 t0 = clock2()
975 for nr in runs:
973 for nr in runs:
976 run()
974 run()
977 t1 = clock2()
975 t1 = clock2()
978 t_usr = t1[0] - t0[0]
976 t_usr = t1[0] - t0[0]
979 t_sys = t1[1] - t0[1]
977 t_sys = t1[1] - t0[1]
980 print("\nIPython CPU timings (estimated):")
978 print("\nIPython CPU timings (estimated):")
981 print("Total runs performed:", nruns)
979 print("Total runs performed:", nruns)
982 print(" Times : %10s %10s" % ('Total', 'Per run'))
980 print(" Times : %10s %10s" % ('Total', 'Per run'))
983 print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns))
981 print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns))
984 print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns))
982 print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns))
985 twall1 = time.perf_counter()
983 twall1 = time.perf_counter()
986 print("Wall time: %10.2f s." % (twall1 - twall0))
984 print("Wall time: %10.2f s." % (twall1 - twall0))
987
985
988 @skip_doctest
986 @skip_doctest
989 @no_var_expand
987 @no_var_expand
990 @line_cell_magic
988 @line_cell_magic
991 @needs_local_scope
989 @needs_local_scope
992 def timeit(self, line='', cell=None, local_ns=None):
990 def timeit(self, line='', cell=None, local_ns=None):
993 """Time execution of a Python statement or expression
991 """Time execution of a Python statement or expression
994
992
995 Usage, in line mode:
993 Usage, in line mode:
996 %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
994 %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
997 or in cell mode:
995 or in cell mode:
998 %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
996 %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
999 code
997 code
1000 code...
998 code...
1001
999
1002 Time execution of a Python statement or expression using the timeit
1000 Time execution of a Python statement or expression using the timeit
1003 module. This function can be used both as a line and cell magic:
1001 module. This function can be used both as a line and cell magic:
1004
1002
1005 - In line mode you can time a single-line statement (though multiple
1003 - In line mode you can time a single-line statement (though multiple
1006 ones can be chained with using semicolons).
1004 ones can be chained with using semicolons).
1007
1005
1008 - In cell mode, the statement in the first line is used as setup code
1006 - In cell mode, the statement in the first line is used as setup code
1009 (executed but not timed) and the body of the cell is timed. The cell
1007 (executed but not timed) and the body of the cell is timed. The cell
1010 body has access to any variables created in the setup code.
1008 body has access to any variables created in the setup code.
1011
1009
1012 Options:
1010 Options:
1013 -n<N>: execute the given statement <N> times in a loop. If <N> is not
1011 -n<N>: execute the given statement <N> times in a loop. If <N> is not
1014 provided, <N> is determined so as to get sufficient accuracy.
1012 provided, <N> is determined so as to get sufficient accuracy.
1015
1013
1016 -r<R>: number of repeats <R>, each consisting of <N> loops, and take the
1014 -r<R>: number of repeats <R>, each consisting of <N> loops, and take the
1017 best result.
1015 best result.
1018 Default: 7
1016 Default: 7
1019
1017
1020 -t: use time.time to measure the time, which is the default on Unix.
1018 -t: use time.time to measure the time, which is the default on Unix.
1021 This function measures wall time.
1019 This function measures wall time.
1022
1020
1023 -c: use time.clock to measure the time, which is the default on
1021 -c: use time.clock to measure the time, which is the default on
1024 Windows and measures wall time. On Unix, resource.getrusage is used
1022 Windows and measures wall time. On Unix, resource.getrusage is used
1025 instead and returns the CPU user time.
1023 instead and returns the CPU user time.
1026
1024
1027 -p<P>: use a precision of <P> digits to display the timing result.
1025 -p<P>: use a precision of <P> digits to display the timing result.
1028 Default: 3
1026 Default: 3
1029
1027
1030 -q: Quiet, do not print result.
1028 -q: Quiet, do not print result.
1031
1029
1032 -o: return a TimeitResult that can be stored in a variable to inspect
1030 -o: return a TimeitResult that can be stored in a variable to inspect
1033 the result in more details.
1031 the result in more details.
1034
1032
1035 .. versionchanged:: 7.3
1033 .. versionchanged:: 7.3
1036 User variables are no longer expanded,
1034 User variables are no longer expanded,
1037 the magic line is always left unmodified.
1035 the magic line is always left unmodified.
1038
1036
1039 Examples
1037 Examples
1040 --------
1038 --------
1041 ::
1039 ::
1042
1040
1043 In [1]: %timeit pass
1041 In [1]: %timeit pass
1044 8.26 ns Β± 0.12 ns per loop (mean Β± std. dev. of 7 runs, 100000000 loops each)
1042 8.26 ns Β± 0.12 ns per loop (mean Β± std. dev. of 7 runs, 100000000 loops each)
1045
1043
1046 In [2]: u = None
1044 In [2]: u = None
1047
1045
1048 In [3]: %timeit u is None
1046 In [3]: %timeit u is None
1049 29.9 ns Β± 0.643 ns per loop (mean Β± std. dev. of 7 runs, 10000000 loops each)
1047 29.9 ns Β± 0.643 ns per loop (mean Β± std. dev. of 7 runs, 10000000 loops each)
1050
1048
1051 In [4]: %timeit -r 4 u == None
1049 In [4]: %timeit -r 4 u == None
1052
1050
1053 In [5]: import time
1051 In [5]: import time
1054
1052
1055 In [6]: %timeit -n1 time.sleep(2)
1053 In [6]: %timeit -n1 time.sleep(2)
1056
1054
1057
1055
1058 The times reported by %timeit will be slightly higher than those
1056 The times reported by %timeit will be slightly higher than those
1059 reported by the timeit.py script when variables are accessed. This is
1057 reported by the timeit.py script when variables are accessed. This is
1060 due to the fact that %timeit executes the statement in the namespace
1058 due to the fact that %timeit executes the statement in the namespace
1061 of the shell, compared with timeit.py, which uses a single setup
1059 of the shell, compared with timeit.py, which uses a single setup
1062 statement to import function or create variables. Generally, the bias
1060 statement to import function or create variables. Generally, the bias
1063 does not matter as long as results from timeit.py are not mixed with
1061 does not matter as long as results from timeit.py are not mixed with
1064 those from %timeit."""
1062 those from %timeit."""
1065
1063
1066 opts, stmt = self.parse_options(line,'n:r:tcp:qo',
1064 opts, stmt = self.parse_options(line,'n:r:tcp:qo',
1067 posix=False, strict=False)
1065 posix=False, strict=False)
1068 if stmt == "" and cell is None:
1066 if stmt == "" and cell is None:
1069 return
1067 return
1070
1068
1071 timefunc = timeit.default_timer
1069 timefunc = timeit.default_timer
1072 number = int(getattr(opts, "n", 0))
1070 number = int(getattr(opts, "n", 0))
1073 default_repeat = 7 if timeit.default_repeat < 7 else timeit.default_repeat
1071 default_repeat = 7 if timeit.default_repeat < 7 else timeit.default_repeat
1074 repeat = int(getattr(opts, "r", default_repeat))
1072 repeat = int(getattr(opts, "r", default_repeat))
1075 precision = int(getattr(opts, "p", 3))
1073 precision = int(getattr(opts, "p", 3))
1076 quiet = 'q' in opts
1074 quiet = 'q' in opts
1077 return_result = 'o' in opts
1075 return_result = 'o' in opts
1078 if hasattr(opts, "t"):
1076 if hasattr(opts, "t"):
1079 timefunc = time.time
1077 timefunc = time.time
1080 if hasattr(opts, "c"):
1078 if hasattr(opts, "c"):
1081 timefunc = clock
1079 timefunc = clock
1082
1080
1083 timer = Timer(timer=timefunc)
1081 timer = Timer(timer=timefunc)
1084 # this code has tight coupling to the inner workings of timeit.Timer,
1082 # this code has tight coupling to the inner workings of timeit.Timer,
1085 # but is there a better way to achieve that the code stmt has access
1083 # but is there a better way to achieve that the code stmt has access
1086 # to the shell namespace?
1084 # to the shell namespace?
1087 transform = self.shell.transform_cell
1085 transform = self.shell.transform_cell
1088
1086
1089 if cell is None:
1087 if cell is None:
1090 # called as line magic
1088 # called as line magic
1091 ast_setup = self.shell.compile.ast_parse("pass")
1089 ast_setup = self.shell.compile.ast_parse("pass")
1092 ast_stmt = self.shell.compile.ast_parse(transform(stmt))
1090 ast_stmt = self.shell.compile.ast_parse(transform(stmt))
1093 else:
1091 else:
1094 ast_setup = self.shell.compile.ast_parse(transform(stmt))
1092 ast_setup = self.shell.compile.ast_parse(transform(stmt))
1095 ast_stmt = self.shell.compile.ast_parse(transform(cell))
1093 ast_stmt = self.shell.compile.ast_parse(transform(cell))
1096
1094
1097 ast_setup = self.shell.transform_ast(ast_setup)
1095 ast_setup = self.shell.transform_ast(ast_setup)
1098 ast_stmt = self.shell.transform_ast(ast_stmt)
1096 ast_stmt = self.shell.transform_ast(ast_stmt)
1099
1097
1100 # Check that these compile to valid Python code *outside* the timer func
1098 # Check that these compile to valid Python code *outside* the timer func
1101 # Invalid code may become valid when put inside the function & loop,
1099 # Invalid code may become valid when put inside the function & loop,
1102 # which messes up error messages.
1100 # which messes up error messages.
1103 # https://github.com/ipython/ipython/issues/10636
1101 # https://github.com/ipython/ipython/issues/10636
1104 self.shell.compile(ast_setup, "<magic-timeit-setup>", "exec")
1102 self.shell.compile(ast_setup, "<magic-timeit-setup>", "exec")
1105 self.shell.compile(ast_stmt, "<magic-timeit-stmt>", "exec")
1103 self.shell.compile(ast_stmt, "<magic-timeit-stmt>", "exec")
1106
1104
1107 # This codestring is taken from timeit.template - we fill it in as an
1105 # This codestring is taken from timeit.template - we fill it in as an
1108 # AST, so that we can apply our AST transformations to the user code
1106 # AST, so that we can apply our AST transformations to the user code
1109 # without affecting the timing code.
1107 # without affecting the timing code.
1110 timeit_ast_template = ast.parse('def inner(_it, _timer):\n'
1108 timeit_ast_template = ast.parse('def inner(_it, _timer):\n'
1111 ' setup\n'
1109 ' setup\n'
1112 ' _t0 = _timer()\n'
1110 ' _t0 = _timer()\n'
1113 ' for _i in _it:\n'
1111 ' for _i in _it:\n'
1114 ' stmt\n'
1112 ' stmt\n'
1115 ' _t1 = _timer()\n'
1113 ' _t1 = _timer()\n'
1116 ' return _t1 - _t0\n')
1114 ' return _t1 - _t0\n')
1117
1115
1118 timeit_ast = TimeitTemplateFiller(ast_setup, ast_stmt).visit(timeit_ast_template)
1116 timeit_ast = TimeitTemplateFiller(ast_setup, ast_stmt).visit(timeit_ast_template)
1119 timeit_ast = ast.fix_missing_locations(timeit_ast)
1117 timeit_ast = ast.fix_missing_locations(timeit_ast)
1120
1118
1121 # Track compilation time so it can be reported if too long
1119 # Track compilation time so it can be reported if too long
1122 # Minimum time above which compilation time will be reported
1120 # Minimum time above which compilation time will be reported
1123 tc_min = 0.1
1121 tc_min = 0.1
1124
1122
1125 t0 = clock()
1123 t0 = clock()
1126 code = self.shell.compile(timeit_ast, "<magic-timeit>", "exec")
1124 code = self.shell.compile(timeit_ast, "<magic-timeit>", "exec")
1127 tc = clock()-t0
1125 tc = clock()-t0
1128
1126
1129 ns = {}
1127 ns = {}
1130 glob = self.shell.user_ns
1128 glob = self.shell.user_ns
1131 # handles global vars with same name as local vars. We store them in conflict_globs.
1129 # handles global vars with same name as local vars. We store them in conflict_globs.
1132 conflict_globs = {}
1130 conflict_globs = {}
1133 if local_ns and cell is None:
1131 if local_ns and cell is None:
1134 for var_name, var_val in glob.items():
1132 for var_name, var_val in glob.items():
1135 if var_name in local_ns:
1133 if var_name in local_ns:
1136 conflict_globs[var_name] = var_val
1134 conflict_globs[var_name] = var_val
1137 glob.update(local_ns)
1135 glob.update(local_ns)
1138
1136
1139 exec(code, glob, ns)
1137 exec(code, glob, ns)
1140 timer.inner = ns["inner"]
1138 timer.inner = ns["inner"]
1141
1139
1142 # This is used to check if there is a huge difference between the
1140 # This is used to check if there is a huge difference between the
1143 # best and worst timings.
1141 # best and worst timings.
1144 # Issue: https://github.com/ipython/ipython/issues/6471
1142 # Issue: https://github.com/ipython/ipython/issues/6471
1145 if number == 0:
1143 if number == 0:
1146 # determine number so that 0.2 <= total time < 2.0
1144 # determine number so that 0.2 <= total time < 2.0
1147 for index in range(0, 10):
1145 for index in range(0, 10):
1148 number = 10 ** index
1146 number = 10 ** index
1149 time_number = timer.timeit(number)
1147 time_number = timer.timeit(number)
1150 if time_number >= 0.2:
1148 if time_number >= 0.2:
1151 break
1149 break
1152
1150
1153 all_runs = timer.repeat(repeat, number)
1151 all_runs = timer.repeat(repeat, number)
1154 best = min(all_runs) / number
1152 best = min(all_runs) / number
1155 worst = max(all_runs) / number
1153 worst = max(all_runs) / number
1156 timeit_result = TimeitResult(number, repeat, best, worst, all_runs, tc, precision)
1154 timeit_result = TimeitResult(number, repeat, best, worst, all_runs, tc, precision)
1157
1155
1158 # Restore global vars from conflict_globs
1156 # Restore global vars from conflict_globs
1159 if conflict_globs:
1157 if conflict_globs:
1160 glob.update(conflict_globs)
1158 glob.update(conflict_globs)
1161
1159
1162 if not quiet :
1160 if not quiet :
1163 # Check best timing is greater than zero to avoid a
1161 # Check best timing is greater than zero to avoid a
1164 # ZeroDivisionError.
1162 # ZeroDivisionError.
1165 # In cases where the slowest timing is lesser than a microsecond
1163 # In cases where the slowest timing is lesser than a microsecond
1166 # we assume that it does not really matter if the fastest
1164 # we assume that it does not really matter if the fastest
1167 # timing is 4 times faster than the slowest timing or not.
1165 # timing is 4 times faster than the slowest timing or not.
1168 if worst > 4 * best and best > 0 and worst > 1e-6:
1166 if worst > 4 * best and best > 0 and worst > 1e-6:
1169 print("The slowest run took %0.2f times longer than the "
1167 print("The slowest run took %0.2f times longer than the "
1170 "fastest. This could mean that an intermediate result "
1168 "fastest. This could mean that an intermediate result "
1171 "is being cached." % (worst / best))
1169 "is being cached." % (worst / best))
1172
1170
1173 print( timeit_result )
1171 print( timeit_result )
1174
1172
1175 if tc > tc_min:
1173 if tc > tc_min:
1176 print("Compiler time: %.2f s" % tc)
1174 print("Compiler time: %.2f s" % tc)
1177 if return_result:
1175 if return_result:
1178 return timeit_result
1176 return timeit_result
1179
1177
1180 @skip_doctest
1178 @skip_doctest
1181 @no_var_expand
1179 @no_var_expand
1182 @needs_local_scope
1180 @needs_local_scope
1183 @line_cell_magic
1181 @line_cell_magic
1184 def time(self,line='', cell=None, local_ns=None):
1182 def time(self,line='', cell=None, local_ns=None):
1185 """Time execution of a Python statement or expression.
1183 """Time execution of a Python statement or expression.
1186
1184
1187 The CPU and wall clock times are printed, and the value of the
1185 The CPU and wall clock times are printed, and the value of the
1188 expression (if any) is returned. Note that under Win32, system time
1186 expression (if any) is returned. Note that under Win32, system time
1189 is always reported as 0, since it can not be measured.
1187 is always reported as 0, since it can not be measured.
1190
1188
1191 This function can be used both as a line and cell magic:
1189 This function can be used both as a line and cell magic:
1192
1190
1193 - In line mode you can time a single-line statement (though multiple
1191 - In line mode you can time a single-line statement (though multiple
1194 ones can be chained with using semicolons).
1192 ones can be chained with using semicolons).
1195
1193
1196 - In cell mode, you can time the cell body (a directly
1194 - In cell mode, you can time the cell body (a directly
1197 following statement raises an error).
1195 following statement raises an error).
1198
1196
1199 This function provides very basic timing functionality. Use the timeit
1197 This function provides very basic timing functionality. Use the timeit
1200 magic for more control over the measurement.
1198 magic for more control over the measurement.
1201
1199
1202 .. versionchanged:: 7.3
1200 .. versionchanged:: 7.3
1203 User variables are no longer expanded,
1201 User variables are no longer expanded,
1204 the magic line is always left unmodified.
1202 the magic line is always left unmodified.
1205
1203
1206 Examples
1204 Examples
1207 --------
1205 --------
1208 ::
1206 ::
1209
1207
1210 In [1]: %time 2**128
1208 In [1]: %time 2**128
1211 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1209 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1212 Wall time: 0.00
1210 Wall time: 0.00
1213 Out[1]: 340282366920938463463374607431768211456L
1211 Out[1]: 340282366920938463463374607431768211456L
1214
1212
1215 In [2]: n = 1000000
1213 In [2]: n = 1000000
1216
1214
1217 In [3]: %time sum(range(n))
1215 In [3]: %time sum(range(n))
1218 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1216 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1219 Wall time: 1.37
1217 Wall time: 1.37
1220 Out[3]: 499999500000L
1218 Out[3]: 499999500000L
1221
1219
1222 In [4]: %time print 'hello world'
1220 In [4]: %time print 'hello world'
1223 hello world
1221 hello world
1224 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1222 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1225 Wall time: 0.00
1223 Wall time: 0.00
1226
1224
1227 Note that the time needed by Python to compile the given expression
1225 Note that the time needed by Python to compile the given expression
1228 will be reported if it is more than 0.1s. In this example, the
1226 will be reported if it is more than 0.1s. In this example, the
1229 actual exponentiation is done by Python at compilation time, so while
1227 actual exponentiation is done by Python at compilation time, so while
1230 the expression can take a noticeable amount of time to compute, that
1228 the expression can take a noticeable amount of time to compute, that
1231 time is purely due to the compilation:
1229 time is purely due to the compilation:
1232
1230
1233 In [5]: %time 3**9999;
1231 In [5]: %time 3**9999;
1234 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1232 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1235 Wall time: 0.00 s
1233 Wall time: 0.00 s
1236
1234
1237 In [6]: %time 3**999999;
1235 In [6]: %time 3**999999;
1238 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1236 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1239 Wall time: 0.00 s
1237 Wall time: 0.00 s
1240 Compiler : 0.78 s
1238 Compiler : 0.78 s
1241 """
1239 """
1242
1240
1243 # fail immediately if the given expression can't be compiled
1241 # fail immediately if the given expression can't be compiled
1244
1242
1245 if line and cell:
1243 if line and cell:
1246 raise UsageError("Can't use statement directly after '%%time'!")
1244 raise UsageError("Can't use statement directly after '%%time'!")
1247
1245
1248 if cell:
1246 if cell:
1249 expr = self.shell.transform_cell(cell)
1247 expr = self.shell.transform_cell(cell)
1250 else:
1248 else:
1251 expr = self.shell.transform_cell(line)
1249 expr = self.shell.transform_cell(line)
1252
1250
1253 # Minimum time above which parse time will be reported
1251 # Minimum time above which parse time will be reported
1254 tp_min = 0.1
1252 tp_min = 0.1
1255
1253
1256 t0 = clock()
1254 t0 = clock()
1257 expr_ast = self.shell.compile.ast_parse(expr)
1255 expr_ast = self.shell.compile.ast_parse(expr)
1258 tp = clock()-t0
1256 tp = clock()-t0
1259
1257
1260 # Apply AST transformations
1258 # Apply AST transformations
1261 expr_ast = self.shell.transform_ast(expr_ast)
1259 expr_ast = self.shell.transform_ast(expr_ast)
1262
1260
1263 # Minimum time above which compilation time will be reported
1261 # Minimum time above which compilation time will be reported
1264 tc_min = 0.1
1262 tc_min = 0.1
1265
1263
1266 expr_val=None
1264 expr_val=None
1267 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1265 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1268 mode = 'eval'
1266 mode = 'eval'
1269 source = '<timed eval>'
1267 source = '<timed eval>'
1270 expr_ast = ast.Expression(expr_ast.body[0].value)
1268 expr_ast = ast.Expression(expr_ast.body[0].value)
1271 else:
1269 else:
1272 mode = 'exec'
1270 mode = 'exec'
1273 source = '<timed exec>'
1271 source = '<timed exec>'
1274 # multi-line %%time case
1272 # multi-line %%time case
1275 if len(expr_ast.body) > 1 and isinstance(expr_ast.body[-1], ast.Expr):
1273 if len(expr_ast.body) > 1 and isinstance(expr_ast.body[-1], ast.Expr):
1276 expr_val= expr_ast.body[-1]
1274 expr_val= expr_ast.body[-1]
1277 expr_ast = expr_ast.body[:-1]
1275 expr_ast = expr_ast.body[:-1]
1278 expr_ast = Module(expr_ast, [])
1276 expr_ast = Module(expr_ast, [])
1279 expr_val = ast.Expression(expr_val.value)
1277 expr_val = ast.Expression(expr_val.value)
1280
1278
1281 t0 = clock()
1279 t0 = clock()
1282 code = self.shell.compile(expr_ast, source, mode)
1280 code = self.shell.compile(expr_ast, source, mode)
1283 tc = clock()-t0
1281 tc = clock()-t0
1284
1282
1285 # skew measurement as little as possible
1283 # skew measurement as little as possible
1286 glob = self.shell.user_ns
1284 glob = self.shell.user_ns
1287 wtime = time.time
1285 wtime = time.time
1288 # time execution
1286 # time execution
1289 wall_st = wtime()
1287 wall_st = wtime()
1290 if mode=='eval':
1288 if mode=='eval':
1291 st = clock2()
1289 st = clock2()
1292 try:
1290 try:
1293 out = eval(code, glob, local_ns)
1291 out = eval(code, glob, local_ns)
1294 except:
1292 except:
1295 self.shell.showtraceback()
1293 self.shell.showtraceback()
1296 return
1294 return
1297 end = clock2()
1295 end = clock2()
1298 else:
1296 else:
1299 st = clock2()
1297 st = clock2()
1300 try:
1298 try:
1301 exec(code, glob, local_ns)
1299 exec(code, glob, local_ns)
1302 out=None
1300 out=None
1303 # multi-line %%time case
1301 # multi-line %%time case
1304 if expr_val is not None:
1302 if expr_val is not None:
1305 code_2 = self.shell.compile(expr_val, source, 'eval')
1303 code_2 = self.shell.compile(expr_val, source, 'eval')
1306 out = eval(code_2, glob, local_ns)
1304 out = eval(code_2, glob, local_ns)
1307 except:
1305 except:
1308 self.shell.showtraceback()
1306 self.shell.showtraceback()
1309 return
1307 return
1310 end = clock2()
1308 end = clock2()
1311
1309
1312 wall_end = wtime()
1310 wall_end = wtime()
1313 # Compute actual times and report
1311 # Compute actual times and report
1314 wall_time = wall_end-wall_st
1312 wall_time = wall_end-wall_st
1315 cpu_user = end[0]-st[0]
1313 cpu_user = end[0]-st[0]
1316 cpu_sys = end[1]-st[1]
1314 cpu_sys = end[1]-st[1]
1317 cpu_tot = cpu_user+cpu_sys
1315 cpu_tot = cpu_user+cpu_sys
1318 # On windows cpu_sys is always zero, so no new information to the next print
1316 # On windows cpu_sys is always zero, so no new information to the next print
1319 if sys.platform != 'win32':
1317 if sys.platform != 'win32':
1320 print("CPU times: user %s, sys: %s, total: %s" % \
1318 print("CPU times: user %s, sys: %s, total: %s" % \
1321 (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot)))
1319 (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot)))
1322 print("Wall time: %s" % _format_time(wall_time))
1320 print("Wall time: %s" % _format_time(wall_time))
1323 if tc > tc_min:
1321 if tc > tc_min:
1324 print("Compiler : %s" % _format_time(tc))
1322 print("Compiler : %s" % _format_time(tc))
1325 if tp > tp_min:
1323 if tp > tp_min:
1326 print("Parser : %s" % _format_time(tp))
1324 print("Parser : %s" % _format_time(tp))
1327 return out
1325 return out
1328
1326
1329 @skip_doctest
1327 @skip_doctest
1330 @line_magic
1328 @line_magic
1331 def macro(self, parameter_s=''):
1329 def macro(self, parameter_s=''):
1332 """Define a macro for future re-execution. It accepts ranges of history,
1330 """Define a macro for future re-execution. It accepts ranges of history,
1333 filenames or string objects.
1331 filenames or string objects.
1334
1332
1335 Usage:\\
1333 Usage:\\
1336 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1334 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1337
1335
1338 Options:
1336 Options:
1339
1337
1340 -r: use 'raw' input. By default, the 'processed' history is used,
1338 -r: use 'raw' input. By default, the 'processed' history is used,
1341 so that magics are loaded in their transformed version to valid
1339 so that magics are loaded in their transformed version to valid
1342 Python. If this option is given, the raw input as typed at the
1340 Python. If this option is given, the raw input as typed at the
1343 command line is used instead.
1341 command line is used instead.
1344
1342
1345 -q: quiet macro definition. By default, a tag line is printed
1343 -q: quiet macro definition. By default, a tag line is printed
1346 to indicate the macro has been created, and then the contents of
1344 to indicate the macro has been created, and then the contents of
1347 the macro are printed. If this option is given, then no printout
1345 the macro are printed. If this option is given, then no printout
1348 is produced once the macro is created.
1346 is produced once the macro is created.
1349
1347
1350 This will define a global variable called `name` which is a string
1348 This will define a global variable called `name` which is a string
1351 made of joining the slices and lines you specify (n1,n2,... numbers
1349 made of joining the slices and lines you specify (n1,n2,... numbers
1352 above) from your input history into a single string. This variable
1350 above) from your input history into a single string. This variable
1353 acts like an automatic function which re-executes those lines as if
1351 acts like an automatic function which re-executes those lines as if
1354 you had typed them. You just type 'name' at the prompt and the code
1352 you had typed them. You just type 'name' at the prompt and the code
1355 executes.
1353 executes.
1356
1354
1357 The syntax for indicating input ranges is described in %history.
1355 The syntax for indicating input ranges is described in %history.
1358
1356
1359 Note: as a 'hidden' feature, you can also use traditional python slice
1357 Note: as a 'hidden' feature, you can also use traditional python slice
1360 notation, where N:M means numbers N through M-1.
1358 notation, where N:M means numbers N through M-1.
1361
1359
1362 For example, if your history contains (print using %hist -n )::
1360 For example, if your history contains (print using %hist -n )::
1363
1361
1364 44: x=1
1362 44: x=1
1365 45: y=3
1363 45: y=3
1366 46: z=x+y
1364 46: z=x+y
1367 47: print x
1365 47: print x
1368 48: a=5
1366 48: a=5
1369 49: print 'x',x,'y',y
1367 49: print 'x',x,'y',y
1370
1368
1371 you can create a macro with lines 44 through 47 (included) and line 49
1369 you can create a macro with lines 44 through 47 (included) and line 49
1372 called my_macro with::
1370 called my_macro with::
1373
1371
1374 In [55]: %macro my_macro 44-47 49
1372 In [55]: %macro my_macro 44-47 49
1375
1373
1376 Now, typing `my_macro` (without quotes) will re-execute all this code
1374 Now, typing `my_macro` (without quotes) will re-execute all this code
1377 in one pass.
1375 in one pass.
1378
1376
1379 You don't need to give the line-numbers in order, and any given line
1377 You don't need to give the line-numbers in order, and any given line
1380 number can appear multiple times. You can assemble macros with any
1378 number can appear multiple times. You can assemble macros with any
1381 lines from your input history in any order.
1379 lines from your input history in any order.
1382
1380
1383 The macro is a simple object which holds its value in an attribute,
1381 The macro is a simple object which holds its value in an attribute,
1384 but IPython's display system checks for macros and executes them as
1382 but IPython's display system checks for macros and executes them as
1385 code instead of printing them when you type their name.
1383 code instead of printing them when you type their name.
1386
1384
1387 You can view a macro's contents by explicitly printing it with::
1385 You can view a macro's contents by explicitly printing it with::
1388
1386
1389 print macro_name
1387 print macro_name
1390
1388
1391 """
1389 """
1392 opts,args = self.parse_options(parameter_s,'rq',mode='list')
1390 opts,args = self.parse_options(parameter_s,'rq',mode='list')
1393 if not args: # List existing macros
1391 if not args: # List existing macros
1394 return sorted(k for k,v in self.shell.user_ns.items() if isinstance(v, Macro))
1392 return sorted(k for k,v in self.shell.user_ns.items() if isinstance(v, Macro))
1395 if len(args) == 1:
1393 if len(args) == 1:
1396 raise UsageError(
1394 raise UsageError(
1397 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1395 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1398 name, codefrom = args[0], " ".join(args[1:])
1396 name, codefrom = args[0], " ".join(args[1:])
1399
1397
1400 #print 'rng',ranges # dbg
1398 #print 'rng',ranges # dbg
1401 try:
1399 try:
1402 lines = self.shell.find_user_code(codefrom, 'r' in opts)
1400 lines = self.shell.find_user_code(codefrom, 'r' in opts)
1403 except (ValueError, TypeError) as e:
1401 except (ValueError, TypeError) as e:
1404 print(e.args[0])
1402 print(e.args[0])
1405 return
1403 return
1406 macro = Macro(lines)
1404 macro = Macro(lines)
1407 self.shell.define_macro(name, macro)
1405 self.shell.define_macro(name, macro)
1408 if not ( 'q' in opts) :
1406 if not ( 'q' in opts) :
1409 print('Macro `%s` created. To execute, type its name (without quotes).' % name)
1407 print('Macro `%s` created. To execute, type its name (without quotes).' % name)
1410 print('=== Macro contents: ===')
1408 print('=== Macro contents: ===')
1411 print(macro, end=' ')
1409 print(macro, end=' ')
1412
1410
1413 @magic_arguments.magic_arguments()
1411 @magic_arguments.magic_arguments()
1414 @magic_arguments.argument('output', type=str, default='', nargs='?',
1412 @magic_arguments.argument('output', type=str, default='', nargs='?',
1415 help="""The name of the variable in which to store output.
1413 help="""The name of the variable in which to store output.
1416 This is a utils.io.CapturedIO object with stdout/err attributes
1414 This is a utils.io.CapturedIO object with stdout/err attributes
1417 for the text of the captured output.
1415 for the text of the captured output.
1418
1416
1419 CapturedOutput also has a show() method for displaying the output,
1417 CapturedOutput also has a show() method for displaying the output,
1420 and __call__ as well, so you can use that to quickly display the
1418 and __call__ as well, so you can use that to quickly display the
1421 output.
1419 output.
1422
1420
1423 If unspecified, captured output is discarded.
1421 If unspecified, captured output is discarded.
1424 """
1422 """
1425 )
1423 )
1426 @magic_arguments.argument('--no-stderr', action="store_true",
1424 @magic_arguments.argument('--no-stderr', action="store_true",
1427 help="""Don't capture stderr."""
1425 help="""Don't capture stderr."""
1428 )
1426 )
1429 @magic_arguments.argument('--no-stdout', action="store_true",
1427 @magic_arguments.argument('--no-stdout', action="store_true",
1430 help="""Don't capture stdout."""
1428 help="""Don't capture stdout."""
1431 )
1429 )
1432 @magic_arguments.argument('--no-display', action="store_true",
1430 @magic_arguments.argument('--no-display', action="store_true",
1433 help="""Don't capture IPython's rich display."""
1431 help="""Don't capture IPython's rich display."""
1434 )
1432 )
1435 @cell_magic
1433 @cell_magic
1436 def capture(self, line, cell):
1434 def capture(self, line, cell):
1437 """run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
1435 """run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
1438 args = magic_arguments.parse_argstring(self.capture, line)
1436 args = magic_arguments.parse_argstring(self.capture, line)
1439 out = not args.no_stdout
1437 out = not args.no_stdout
1440 err = not args.no_stderr
1438 err = not args.no_stderr
1441 disp = not args.no_display
1439 disp = not args.no_display
1442 with capture_output(out, err, disp) as io:
1440 with capture_output(out, err, disp) as io:
1443 self.shell.run_cell(cell)
1441 self.shell.run_cell(cell)
1444 if args.output:
1442 if args.output:
1445 self.shell.user_ns[args.output] = io
1443 self.shell.user_ns[args.output] = io
1446
1444
1447 def parse_breakpoint(text, current_file):
1445 def parse_breakpoint(text, current_file):
1448 '''Returns (file, line) for file:line and (current_file, line) for line'''
1446 '''Returns (file, line) for file:line and (current_file, line) for line'''
1449 colon = text.find(':')
1447 colon = text.find(':')
1450 if colon == -1:
1448 if colon == -1:
1451 return current_file, int(text)
1449 return current_file, int(text)
1452 else:
1450 else:
1453 return text[:colon], int(text[colon+1:])
1451 return text[:colon], int(text[colon+1:])
1454
1452
1455 def _format_time(timespan, precision=3):
1453 def _format_time(timespan, precision=3):
1456 """Formats the timespan in a human readable form"""
1454 """Formats the timespan in a human readable form"""
1457
1455
1458 if timespan >= 60.0:
1456 if timespan >= 60.0:
1459 # we have more than a minute, format that in a human readable form
1457 # we have more than a minute, format that in a human readable form
1460 # Idea from http://snipplr.com/view/5713/
1458 # Idea from http://snipplr.com/view/5713/
1461 parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
1459 parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
1462 time = []
1460 time = []
1463 leftover = timespan
1461 leftover = timespan
1464 for suffix, length in parts:
1462 for suffix, length in parts:
1465 value = int(leftover / length)
1463 value = int(leftover / length)
1466 if value > 0:
1464 if value > 0:
1467 leftover = leftover % length
1465 leftover = leftover % length
1468 time.append(u'%s%s' % (str(value), suffix))
1466 time.append(u'%s%s' % (str(value), suffix))
1469 if leftover < 1:
1467 if leftover < 1:
1470 break
1468 break
1471 return " ".join(time)
1469 return " ".join(time)
1472
1470
1473
1471
1474 # Unfortunately the unicode 'micro' symbol can cause problems in
1472 # Unfortunately the unicode 'micro' symbol can cause problems in
1475 # certain terminals.
1473 # certain terminals.
1476 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1474 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1477 # Try to prevent crashes by being more secure than it needs to
1475 # Try to prevent crashes by being more secure than it needs to
1478 # E.g. eclipse is able to print a Β΅, but has no sys.stdout.encoding set.
1476 # E.g. eclipse is able to print a Β΅, but has no sys.stdout.encoding set.
1479 units = [u"s", u"ms",u'us',"ns"] # the save value
1477 units = [u"s", u"ms",u'us',"ns"] # the save value
1480 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
1478 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
1481 try:
1479 try:
1482 u'\xb5'.encode(sys.stdout.encoding)
1480 u'\xb5'.encode(sys.stdout.encoding)
1483 units = [u"s", u"ms",u'\xb5s',"ns"]
1481 units = [u"s", u"ms",u'\xb5s',"ns"]
1484 except:
1482 except:
1485 pass
1483 pass
1486 scaling = [1, 1e3, 1e6, 1e9]
1484 scaling = [1, 1e3, 1e6, 1e9]
1487
1485
1488 if timespan > 0.0:
1486 if timespan > 0.0:
1489 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1487 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1490 else:
1488 else:
1491 order = 3
1489 order = 3
1492 return u"%.*g %s" % (precision, timespan * scaling[order], units[order])
1490 return u"%.*g %s" % (precision, timespan * scaling[order], units[order])
@@ -1,582 +1,601 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """Tests for code execution (%run and related), which is particularly tricky.
2 """Tests for code execution (%run and related), which is particularly tricky.
3
3
4 Because of how %run manages namespaces, and the fact that we are trying here to
4 Because of how %run manages namespaces, and the fact that we are trying here to
5 verify subtle object deletion and reference counting issues, the %run tests
5 verify subtle object deletion and reference counting issues, the %run tests
6 will be kept in this separate file. This makes it easier to aggregate in one
6 will be kept in this separate file. This makes it easier to aggregate in one
7 place the tricks needed to handle it; most other magics are much easier to test
7 place the tricks needed to handle it; most other magics are much easier to test
8 and we do so in a common test_magic file.
8 and we do so in a common test_magic file.
9
9
10 Note that any test using `run -i` should make sure to do a `reset` afterwards,
10 Note that any test using `run -i` should make sure to do a `reset` afterwards,
11 as otherwise it may influence later tests.
11 as otherwise it may influence later tests.
12 """
12 """
13
13
14 # Copyright (c) IPython Development Team.
14 # Copyright (c) IPython Development Team.
15 # Distributed under the terms of the Modified BSD License.
15 # Distributed under the terms of the Modified BSD License.
16
16
17
17
18
18
19 import functools
19 import functools
20 import os
20 import os
21 from os.path import join as pjoin
21 from os.path import join as pjoin
22 import random
22 import random
23 import string
23 import string
24 import sys
24 import sys
25 import textwrap
25 import textwrap
26 import unittest
26 import unittest
27 from unittest.mock import patch
27 from unittest.mock import patch
28
28
29 import nose.tools as nt
29 import nose.tools as nt
30 from nose import SkipTest
30 from nose import SkipTest
31
31
32 from IPython.testing import decorators as dec
32 from IPython.testing import decorators as dec
33 from IPython.testing import tools as tt
33 from IPython.testing import tools as tt
34 from IPython.utils.io import capture_output
34 from IPython.utils.io import capture_output
35 from IPython.utils.tempdir import TemporaryDirectory
35 from IPython.utils.tempdir import TemporaryDirectory
36 from IPython.core import debugger
36 from IPython.core import debugger
37
37
38 def doctest_refbug():
38 def doctest_refbug():
39 """Very nasty problem with references held by multiple runs of a script.
39 """Very nasty problem with references held by multiple runs of a script.
40 See: https://github.com/ipython/ipython/issues/141
40 See: https://github.com/ipython/ipython/issues/141
41
41
42 In [1]: _ip.clear_main_mod_cache()
42 In [1]: _ip.clear_main_mod_cache()
43 # random
43 # random
44
44
45 In [2]: %run refbug
45 In [2]: %run refbug
46
46
47 In [3]: call_f()
47 In [3]: call_f()
48 lowercased: hello
48 lowercased: hello
49
49
50 In [4]: %run refbug
50 In [4]: %run refbug
51
51
52 In [5]: call_f()
52 In [5]: call_f()
53 lowercased: hello
53 lowercased: hello
54 lowercased: hello
54 lowercased: hello
55 """
55 """
56
56
57
57
58 def doctest_run_builtins():
58 def doctest_run_builtins():
59 r"""Check that %run doesn't damage __builtins__.
59 r"""Check that %run doesn't damage __builtins__.
60
60
61 In [1]: import tempfile
61 In [1]: import tempfile
62
62
63 In [2]: bid1 = id(__builtins__)
63 In [2]: bid1 = id(__builtins__)
64
64
65 In [3]: fname = tempfile.mkstemp('.py')[1]
65 In [3]: fname = tempfile.mkstemp('.py')[1]
66
66
67 In [3]: f = open(fname,'w')
67 In [3]: f = open(fname,'w')
68
68
69 In [4]: dummy= f.write('pass\n')
69 In [4]: dummy= f.write('pass\n')
70
70
71 In [5]: f.flush()
71 In [5]: f.flush()
72
72
73 In [6]: t1 = type(__builtins__)
73 In [6]: t1 = type(__builtins__)
74
74
75 In [7]: %run $fname
75 In [7]: %run $fname
76
76
77 In [7]: f.close()
77 In [7]: f.close()
78
78
79 In [8]: bid2 = id(__builtins__)
79 In [8]: bid2 = id(__builtins__)
80
80
81 In [9]: t2 = type(__builtins__)
81 In [9]: t2 = type(__builtins__)
82
82
83 In [10]: t1 == t2
83 In [10]: t1 == t2
84 Out[10]: True
84 Out[10]: True
85
85
86 In [10]: bid1 == bid2
86 In [10]: bid1 == bid2
87 Out[10]: True
87 Out[10]: True
88
88
89 In [12]: try:
89 In [12]: try:
90 ....: os.unlink(fname)
90 ....: os.unlink(fname)
91 ....: except:
91 ....: except:
92 ....: pass
92 ....: pass
93 ....:
93 ....:
94 """
94 """
95
95
96
96
97 def doctest_run_option_parser():
97 def doctest_run_option_parser():
98 r"""Test option parser in %run.
98 r"""Test option parser in %run.
99
99
100 In [1]: %run print_argv.py
100 In [1]: %run print_argv.py
101 []
101 []
102
102
103 In [2]: %run print_argv.py print*.py
103 In [2]: %run print_argv.py print*.py
104 ['print_argv.py']
104 ['print_argv.py']
105
105
106 In [3]: %run -G print_argv.py print*.py
106 In [3]: %run -G print_argv.py print*.py
107 ['print*.py']
107 ['print*.py']
108
108
109 """
109 """
110
110
111
111
112 @dec.skip_win32
112 @dec.skip_win32
113 def doctest_run_option_parser_for_posix():
113 def doctest_run_option_parser_for_posix():
114 r"""Test option parser in %run (Linux/OSX specific).
114 r"""Test option parser in %run (Linux/OSX specific).
115
115
116 You need double quote to escape glob in POSIX systems:
116 You need double quote to escape glob in POSIX systems:
117
117
118 In [1]: %run print_argv.py print\\*.py
118 In [1]: %run print_argv.py print\\*.py
119 ['print*.py']
119 ['print*.py']
120
120
121 You can't use quote to escape glob in POSIX systems:
121 You can't use quote to escape glob in POSIX systems:
122
122
123 In [2]: %run print_argv.py 'print*.py'
123 In [2]: %run print_argv.py 'print*.py'
124 ['print_argv.py']
124 ['print_argv.py']
125
125
126 """
126 """
127
127
128
128
129 @dec.skip_if_not_win32
129 @dec.skip_if_not_win32
130 def doctest_run_option_parser_for_windows():
130 def doctest_run_option_parser_for_windows():
131 r"""Test option parser in %run (Windows specific).
131 r"""Test option parser in %run (Windows specific).
132
132
133 In Windows, you can't escape ``*` `by backslash:
133 In Windows, you can't escape ``*` `by backslash:
134
134
135 In [1]: %run print_argv.py print\\*.py
135 In [1]: %run print_argv.py print\\*.py
136 ['print\\*.py']
136 ['print\\*.py']
137
137
138 You can use quote to escape glob:
138 You can use quote to escape glob:
139
139
140 In [2]: %run print_argv.py 'print*.py'
140 In [2]: %run print_argv.py 'print*.py'
141 ['print*.py']
141 ['print*.py']
142
142
143 """
143 """
144
144
145
145
146 def doctest_reset_del():
146 def doctest_reset_del():
147 """Test that resetting doesn't cause errors in __del__ methods.
147 """Test that resetting doesn't cause errors in __del__ methods.
148
148
149 In [2]: class A(object):
149 In [2]: class A(object):
150 ...: def __del__(self):
150 ...: def __del__(self):
151 ...: print(str("Hi"))
151 ...: print(str("Hi"))
152 ...:
152 ...:
153
153
154 In [3]: a = A()
154 In [3]: a = A()
155
155
156 In [4]: get_ipython().reset()
156 In [4]: get_ipython().reset()
157 Hi
157 Hi
158
158
159 In [5]: 1+1
159 In [5]: 1+1
160 Out[5]: 2
160 Out[5]: 2
161 """
161 """
162
162
163 # For some tests, it will be handy to organize them in a class with a common
163 # For some tests, it will be handy to organize them in a class with a common
164 # setup that makes a temp file
164 # setup that makes a temp file
165
165
166 class TestMagicRunPass(tt.TempFileMixin):
166 class TestMagicRunPass(tt.TempFileMixin):
167
167
168 def setUp(self):
168 def setUp(self):
169 content = "a = [1,2,3]\nb = 1"
169 content = "a = [1,2,3]\nb = 1"
170 self.mktmp(content)
170 self.mktmp(content)
171
171
172 def run_tmpfile(self):
172 def run_tmpfile(self):
173 _ip = get_ipython()
173 _ip = get_ipython()
174 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
174 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
175 # See below and ticket https://bugs.launchpad.net/bugs/366353
175 # See below and ticket https://bugs.launchpad.net/bugs/366353
176 _ip.magic('run %s' % self.fname)
176 _ip.magic('run %s' % self.fname)
177
177
178 def run_tmpfile_p(self):
178 def run_tmpfile_p(self):
179 _ip = get_ipython()
179 _ip = get_ipython()
180 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
180 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
181 # See below and ticket https://bugs.launchpad.net/bugs/366353
181 # See below and ticket https://bugs.launchpad.net/bugs/366353
182 _ip.magic('run -p %s' % self.fname)
182 _ip.magic('run -p %s' % self.fname)
183
183
184 def test_builtins_id(self):
184 def test_builtins_id(self):
185 """Check that %run doesn't damage __builtins__ """
185 """Check that %run doesn't damage __builtins__ """
186 _ip = get_ipython()
186 _ip = get_ipython()
187 # Test that the id of __builtins__ is not modified by %run
187 # Test that the id of __builtins__ is not modified by %run
188 bid1 = id(_ip.user_ns['__builtins__'])
188 bid1 = id(_ip.user_ns['__builtins__'])
189 self.run_tmpfile()
189 self.run_tmpfile()
190 bid2 = id(_ip.user_ns['__builtins__'])
190 bid2 = id(_ip.user_ns['__builtins__'])
191 nt.assert_equal(bid1, bid2)
191 nt.assert_equal(bid1, bid2)
192
192
193 def test_builtins_type(self):
193 def test_builtins_type(self):
194 """Check that the type of __builtins__ doesn't change with %run.
194 """Check that the type of __builtins__ doesn't change with %run.
195
195
196 However, the above could pass if __builtins__ was already modified to
196 However, the above could pass if __builtins__ was already modified to
197 be a dict (it should be a module) by a previous use of %run. So we
197 be a dict (it should be a module) by a previous use of %run. So we
198 also check explicitly that it really is a module:
198 also check explicitly that it really is a module:
199 """
199 """
200 _ip = get_ipython()
200 _ip = get_ipython()
201 self.run_tmpfile()
201 self.run_tmpfile()
202 nt.assert_equal(type(_ip.user_ns['__builtins__']),type(sys))
202 nt.assert_equal(type(_ip.user_ns['__builtins__']),type(sys))
203
203
204 def test_run_profile( self ):
204 def test_run_profile( self ):
205 """Test that the option -p, which invokes the profiler, do not
205 """Test that the option -p, which invokes the profiler, do not
206 crash by invoking execfile"""
206 crash by invoking execfile"""
207 self.run_tmpfile_p()
207 self.run_tmpfile_p()
208
208
209 def test_run_debug_twice(self):
209 def test_run_debug_twice(self):
210 # https://github.com/ipython/ipython/issues/10028
210 # https://github.com/ipython/ipython/issues/10028
211 _ip = get_ipython()
211 _ip = get_ipython()
212 with tt.fake_input(['c']):
212 with tt.fake_input(['c']):
213 _ip.magic('run -d %s' % self.fname)
213 _ip.magic('run -d %s' % self.fname)
214 with tt.fake_input(['c']):
214 with tt.fake_input(['c']):
215 _ip.magic('run -d %s' % self.fname)
215 _ip.magic('run -d %s' % self.fname)
216
216
217 def test_run_debug_twice_with_breakpoint(self):
217 def test_run_debug_twice_with_breakpoint(self):
218 """Make a valid python temp file."""
218 """Make a valid python temp file."""
219 _ip = get_ipython()
219 _ip = get_ipython()
220 with tt.fake_input(['b 2', 'c', 'c']):
220 with tt.fake_input(['b 2', 'c', 'c']):
221 _ip.magic('run -d %s' % self.fname)
221 _ip.magic('run -d %s' % self.fname)
222
222
223 with tt.fake_input(['c']):
223 with tt.fake_input(['c']):
224 with tt.AssertNotPrints('KeyError'):
224 with tt.AssertNotPrints('KeyError'):
225 _ip.magic('run -d %s' % self.fname)
225 _ip.magic('run -d %s' % self.fname)
226
226
227
227
228 class TestMagicRunSimple(tt.TempFileMixin):
228 class TestMagicRunSimple(tt.TempFileMixin):
229
229
230 def test_simpledef(self):
230 def test_simpledef(self):
231 """Test that simple class definitions work."""
231 """Test that simple class definitions work."""
232 src = ("class foo: pass\n"
232 src = ("class foo: pass\n"
233 "def f(): return foo()")
233 "def f(): return foo()")
234 self.mktmp(src)
234 self.mktmp(src)
235 _ip.magic('run %s' % self.fname)
235 _ip.magic('run %s' % self.fname)
236 _ip.run_cell('t = isinstance(f(), foo)')
236 _ip.run_cell('t = isinstance(f(), foo)')
237 nt.assert_true(_ip.user_ns['t'])
237 nt.assert_true(_ip.user_ns['t'])
238
238
239 def test_obj_del(self):
239 def test_obj_del(self):
240 """Test that object's __del__ methods are called on exit."""
240 """Test that object's __del__ methods are called on exit."""
241 if sys.platform == 'win32':
241 if sys.platform == 'win32':
242 try:
242 try:
243 import win32api
243 import win32api
244 except ImportError:
244 except ImportError:
245 raise SkipTest("Test requires pywin32")
245 raise SkipTest("Test requires pywin32")
246 src = ("class A(object):\n"
246 src = ("class A(object):\n"
247 " def __del__(self):\n"
247 " def __del__(self):\n"
248 " print('object A deleted')\n"
248 " print('object A deleted')\n"
249 "a = A()\n")
249 "a = A()\n")
250 self.mktmp(src)
250 self.mktmp(src)
251 err = None
251 err = None
252 tt.ipexec_validate(self.fname, 'object A deleted', err)
252 tt.ipexec_validate(self.fname, 'object A deleted', err)
253
253
254 def test_aggressive_namespace_cleanup(self):
254 def test_aggressive_namespace_cleanup(self):
255 """Test that namespace cleanup is not too aggressive GH-238
255 """Test that namespace cleanup is not too aggressive GH-238
256
256
257 Returning from another run magic deletes the namespace"""
257 Returning from another run magic deletes the namespace"""
258 # see ticket https://github.com/ipython/ipython/issues/238
258 # see ticket https://github.com/ipython/ipython/issues/238
259
259
260 with tt.TempFileMixin() as empty:
260 with tt.TempFileMixin() as empty:
261 empty.mktmp('')
261 empty.mktmp('')
262 # On Windows, the filename will have \users in it, so we need to use the
262 # On Windows, the filename will have \users in it, so we need to use the
263 # repr so that the \u becomes \\u.
263 # repr so that the \u becomes \\u.
264 src = ("ip = get_ipython()\n"
264 src = ("ip = get_ipython()\n"
265 "for i in range(5):\n"
265 "for i in range(5):\n"
266 " try:\n"
266 " try:\n"
267 " ip.magic(%r)\n"
267 " ip.magic(%r)\n"
268 " except NameError as e:\n"
268 " except NameError as e:\n"
269 " print(i)\n"
269 " print(i)\n"
270 " break\n" % ('run ' + empty.fname))
270 " break\n" % ('run ' + empty.fname))
271 self.mktmp(src)
271 self.mktmp(src)
272 _ip.magic('run %s' % self.fname)
272 _ip.magic('run %s' % self.fname)
273 _ip.run_cell('ip == get_ipython()')
273 _ip.run_cell('ip == get_ipython()')
274 nt.assert_equal(_ip.user_ns['i'], 4)
274 nt.assert_equal(_ip.user_ns['i'], 4)
275
275
276 def test_run_second(self):
276 def test_run_second(self):
277 """Test that running a second file doesn't clobber the first, gh-3547
277 """Test that running a second file doesn't clobber the first, gh-3547
278 """
278 """
279 self.mktmp("avar = 1\n"
279 self.mktmp("avar = 1\n"
280 "def afunc():\n"
280 "def afunc():\n"
281 " return avar\n")
281 " return avar\n")
282
282
283 with tt.TempFileMixin() as empty:
283 with tt.TempFileMixin() as empty:
284 empty.mktmp("")
284 empty.mktmp("")
285
285
286 _ip.magic('run %s' % self.fname)
286 _ip.magic('run %s' % self.fname)
287 _ip.magic('run %s' % empty.fname)
287 _ip.magic('run %s' % empty.fname)
288 nt.assert_equal(_ip.user_ns['afunc'](), 1)
288 nt.assert_equal(_ip.user_ns['afunc'](), 1)
289
289
290 @dec.skip_win32
290 @dec.skip_win32
291 def test_tclass(self):
291 def test_tclass(self):
292 mydir = os.path.dirname(__file__)
292 mydir = os.path.dirname(__file__)
293 tc = os.path.join(mydir, 'tclass')
293 tc = os.path.join(mydir, 'tclass')
294 src = ("%%run '%s' C-first\n"
294 src = ("%%run '%s' C-first\n"
295 "%%run '%s' C-second\n"
295 "%%run '%s' C-second\n"
296 "%%run '%s' C-third\n") % (tc, tc, tc)
296 "%%run '%s' C-third\n") % (tc, tc, tc)
297 self.mktmp(src, '.ipy')
297 self.mktmp(src, '.ipy')
298 out = """\
298 out = """\
299 ARGV 1-: ['C-first']
299 ARGV 1-: ['C-first']
300 ARGV 1-: ['C-second']
300 ARGV 1-: ['C-second']
301 tclass.py: deleting object: C-first
301 tclass.py: deleting object: C-first
302 ARGV 1-: ['C-third']
302 ARGV 1-: ['C-third']
303 tclass.py: deleting object: C-second
303 tclass.py: deleting object: C-second
304 tclass.py: deleting object: C-third
304 tclass.py: deleting object: C-third
305 """
305 """
306 err = None
306 err = None
307 tt.ipexec_validate(self.fname, out, err)
307 tt.ipexec_validate(self.fname, out, err)
308
308
309 def test_run_i_after_reset(self):
309 def test_run_i_after_reset(self):
310 """Check that %run -i still works after %reset (gh-693)"""
310 """Check that %run -i still works after %reset (gh-693)"""
311 src = "yy = zz\n"
311 src = "yy = zz\n"
312 self.mktmp(src)
312 self.mktmp(src)
313 _ip.run_cell("zz = 23")
313 _ip.run_cell("zz = 23")
314 try:
314 try:
315 _ip.magic('run -i %s' % self.fname)
315 _ip.magic('run -i %s' % self.fname)
316 nt.assert_equal(_ip.user_ns['yy'], 23)
316 nt.assert_equal(_ip.user_ns['yy'], 23)
317 finally:
317 finally:
318 _ip.magic('reset -f')
318 _ip.magic('reset -f')
319
319
320 _ip.run_cell("zz = 23")
320 _ip.run_cell("zz = 23")
321 try:
321 try:
322 _ip.magic('run -i %s' % self.fname)
322 _ip.magic('run -i %s' % self.fname)
323 nt.assert_equal(_ip.user_ns['yy'], 23)
323 nt.assert_equal(_ip.user_ns['yy'], 23)
324 finally:
324 finally:
325 _ip.magic('reset -f')
325 _ip.magic('reset -f')
326
326
327 def test_unicode(self):
327 def test_unicode(self):
328 """Check that files in odd encodings are accepted."""
328 """Check that files in odd encodings are accepted."""
329 mydir = os.path.dirname(__file__)
329 mydir = os.path.dirname(__file__)
330 na = os.path.join(mydir, 'nonascii.py')
330 na = os.path.join(mydir, 'nonascii.py')
331 _ip.magic('run "%s"' % na)
331 _ip.magic('run "%s"' % na)
332 nt.assert_equal(_ip.user_ns['u'], u'ΠŽΡ‚β„–Π€')
332 nt.assert_equal(_ip.user_ns['u'], u'ΠŽΡ‚β„–Π€')
333
333
334 def test_run_py_file_attribute(self):
334 def test_run_py_file_attribute(self):
335 """Test handling of `__file__` attribute in `%run <file>.py`."""
335 """Test handling of `__file__` attribute in `%run <file>.py`."""
336 src = "t = __file__\n"
336 src = "t = __file__\n"
337 self.mktmp(src)
337 self.mktmp(src)
338 _missing = object()
338 _missing = object()
339 file1 = _ip.user_ns.get('__file__', _missing)
339 file1 = _ip.user_ns.get('__file__', _missing)
340 _ip.magic('run %s' % self.fname)
340 _ip.magic('run %s' % self.fname)
341 file2 = _ip.user_ns.get('__file__', _missing)
341 file2 = _ip.user_ns.get('__file__', _missing)
342
342
343 # Check that __file__ was equal to the filename in the script's
343 # Check that __file__ was equal to the filename in the script's
344 # namespace.
344 # namespace.
345 nt.assert_equal(_ip.user_ns['t'], self.fname)
345 nt.assert_equal(_ip.user_ns['t'], self.fname)
346
346
347 # Check that __file__ was not leaked back into user_ns.
347 # Check that __file__ was not leaked back into user_ns.
348 nt.assert_equal(file1, file2)
348 nt.assert_equal(file1, file2)
349
349
350 def test_run_ipy_file_attribute(self):
350 def test_run_ipy_file_attribute(self):
351 """Test handling of `__file__` attribute in `%run <file.ipy>`."""
351 """Test handling of `__file__` attribute in `%run <file.ipy>`."""
352 src = "t = __file__\n"
352 src = "t = __file__\n"
353 self.mktmp(src, ext='.ipy')
353 self.mktmp(src, ext='.ipy')
354 _missing = object()
354 _missing = object()
355 file1 = _ip.user_ns.get('__file__', _missing)
355 file1 = _ip.user_ns.get('__file__', _missing)
356 _ip.magic('run %s' % self.fname)
356 _ip.magic('run %s' % self.fname)
357 file2 = _ip.user_ns.get('__file__', _missing)
357 file2 = _ip.user_ns.get('__file__', _missing)
358
358
359 # Check that __file__ was equal to the filename in the script's
359 # Check that __file__ was equal to the filename in the script's
360 # namespace.
360 # namespace.
361 nt.assert_equal(_ip.user_ns['t'], self.fname)
361 nt.assert_equal(_ip.user_ns['t'], self.fname)
362
362
363 # Check that __file__ was not leaked back into user_ns.
363 # Check that __file__ was not leaked back into user_ns.
364 nt.assert_equal(file1, file2)
364 nt.assert_equal(file1, file2)
365
365
366 def test_run_formatting(self):
366 def test_run_formatting(self):
367 """ Test that %run -t -N<N> does not raise a TypeError for N > 1."""
367 """ Test that %run -t -N<N> does not raise a TypeError for N > 1."""
368 src = "pass"
368 src = "pass"
369 self.mktmp(src)
369 self.mktmp(src)
370 _ip.magic('run -t -N 1 %s' % self.fname)
370 _ip.magic('run -t -N 1 %s' % self.fname)
371 _ip.magic('run -t -N 10 %s' % self.fname)
371 _ip.magic('run -t -N 10 %s' % self.fname)
372
372
373 def test_ignore_sys_exit(self):
373 def test_ignore_sys_exit(self):
374 """Test the -e option to ignore sys.exit()"""
374 """Test the -e option to ignore sys.exit()"""
375 src = "import sys; sys.exit(1)"
375 src = "import sys; sys.exit(1)"
376 self.mktmp(src)
376 self.mktmp(src)
377 with tt.AssertPrints('SystemExit'):
377 with tt.AssertPrints('SystemExit'):
378 _ip.magic('run %s' % self.fname)
378 _ip.magic('run %s' % self.fname)
379
379
380 with tt.AssertNotPrints('SystemExit'):
380 with tt.AssertNotPrints('SystemExit'):
381 _ip.magic('run -e %s' % self.fname)
381 _ip.magic('run -e %s' % self.fname)
382
382
383 def test_run_nb(self):
383 def test_run_nb(self):
384 """Test %run notebook.ipynb"""
384 """Test %run notebook.ipynb"""
385 from nbformat import v4, writes
385 from nbformat import v4, writes
386 nb = v4.new_notebook(
386 nb = v4.new_notebook(
387 cells=[
387 cells=[
388 v4.new_markdown_cell("The Ultimate Question of Everything"),
388 v4.new_markdown_cell("The Ultimate Question of Everything"),
389 v4.new_code_cell("answer=42")
389 v4.new_code_cell("answer=42")
390 ]
390 ]
391 )
391 )
392 src = writes(nb, version=4)
392 src = writes(nb, version=4)
393 self.mktmp(src, ext='.ipynb')
393 self.mktmp(src, ext='.ipynb')
394
394
395 _ip.magic("run %s" % self.fname)
395 _ip.magic("run %s" % self.fname)
396
396
397 nt.assert_equal(_ip.user_ns['answer'], 42)
397 nt.assert_equal(_ip.user_ns['answer'], 42)
398
398
399 def test_run_nb_error(self):
400 """Test %run notebook.ipynb error"""
401 from nbformat import v4, writes
402 # %run when a file name isn't provided
403 nt.assert_raises(Exception, _ip.magic, "run")
404
405 # %run when a file doesn't exist
406 nt.assert_raises(Exception, _ip.magic, "run foobar.ipynb")
407
408 # %run on a notebook with an error
409 nb = v4.new_notebook(
410 cells=[
411 v4.new_code_cell("0/0")
412 ]
413 )
414 src = writes(nb, version=4)
415 self.mktmp(src, ext='.ipynb')
416 nt.assert_raises(Exception, _ip.magic, "run %s" % self.fname)
417
399 def test_file_options(self):
418 def test_file_options(self):
400 src = ('import sys\n'
419 src = ('import sys\n'
401 'a = " ".join(sys.argv[1:])\n')
420 'a = " ".join(sys.argv[1:])\n')
402 self.mktmp(src)
421 self.mktmp(src)
403 test_opts = '-x 3 --verbose'
422 test_opts = '-x 3 --verbose'
404 _ip.run_line_magic("run", '{0} {1}'.format(self.fname, test_opts))
423 _ip.run_line_magic("run", '{0} {1}'.format(self.fname, test_opts))
405 nt.assert_equal(_ip.user_ns['a'], test_opts)
424 nt.assert_equal(_ip.user_ns['a'], test_opts)
406
425
407
426
408 class TestMagicRunWithPackage(unittest.TestCase):
427 class TestMagicRunWithPackage(unittest.TestCase):
409
428
410 def writefile(self, name, content):
429 def writefile(self, name, content):
411 path = os.path.join(self.tempdir.name, name)
430 path = os.path.join(self.tempdir.name, name)
412 d = os.path.dirname(path)
431 d = os.path.dirname(path)
413 if not os.path.isdir(d):
432 if not os.path.isdir(d):
414 os.makedirs(d)
433 os.makedirs(d)
415 with open(path, 'w') as f:
434 with open(path, 'w') as f:
416 f.write(textwrap.dedent(content))
435 f.write(textwrap.dedent(content))
417
436
418 def setUp(self):
437 def setUp(self):
419 self.package = package = 'tmp{0}'.format(''.join([random.choice(string.ascii_letters) for i in range(10)]))
438 self.package = package = 'tmp{0}'.format(''.join([random.choice(string.ascii_letters) for i in range(10)]))
420 """Temporary (probably) valid python package name."""
439 """Temporary (probably) valid python package name."""
421
440
422 self.value = int(random.random() * 10000)
441 self.value = int(random.random() * 10000)
423
442
424 self.tempdir = TemporaryDirectory()
443 self.tempdir = TemporaryDirectory()
425 self.__orig_cwd = os.getcwd()
444 self.__orig_cwd = os.getcwd()
426 sys.path.insert(0, self.tempdir.name)
445 sys.path.insert(0, self.tempdir.name)
427
446
428 self.writefile(os.path.join(package, '__init__.py'), '')
447 self.writefile(os.path.join(package, '__init__.py'), '')
429 self.writefile(os.path.join(package, 'sub.py'), """
448 self.writefile(os.path.join(package, 'sub.py'), """
430 x = {0!r}
449 x = {0!r}
431 """.format(self.value))
450 """.format(self.value))
432 self.writefile(os.path.join(package, 'relative.py'), """
451 self.writefile(os.path.join(package, 'relative.py'), """
433 from .sub import x
452 from .sub import x
434 """)
453 """)
435 self.writefile(os.path.join(package, 'absolute.py'), """
454 self.writefile(os.path.join(package, 'absolute.py'), """
436 from {0}.sub import x
455 from {0}.sub import x
437 """.format(package))
456 """.format(package))
438 self.writefile(os.path.join(package, 'args.py'), """
457 self.writefile(os.path.join(package, 'args.py'), """
439 import sys
458 import sys
440 a = " ".join(sys.argv[1:])
459 a = " ".join(sys.argv[1:])
441 """.format(package))
460 """.format(package))
442
461
443 def tearDown(self):
462 def tearDown(self):
444 os.chdir(self.__orig_cwd)
463 os.chdir(self.__orig_cwd)
445 sys.path[:] = [p for p in sys.path if p != self.tempdir.name]
464 sys.path[:] = [p for p in sys.path if p != self.tempdir.name]
446 self.tempdir.cleanup()
465 self.tempdir.cleanup()
447
466
448 def check_run_submodule(self, submodule, opts=''):
467 def check_run_submodule(self, submodule, opts=''):
449 _ip.user_ns.pop('x', None)
468 _ip.user_ns.pop('x', None)
450 _ip.magic('run {2} -m {0}.{1}'.format(self.package, submodule, opts))
469 _ip.magic('run {2} -m {0}.{1}'.format(self.package, submodule, opts))
451 self.assertEqual(_ip.user_ns['x'], self.value,
470 self.assertEqual(_ip.user_ns['x'], self.value,
452 'Variable `x` is not loaded from module `{0}`.'
471 'Variable `x` is not loaded from module `{0}`.'
453 .format(submodule))
472 .format(submodule))
454
473
455 def test_run_submodule_with_absolute_import(self):
474 def test_run_submodule_with_absolute_import(self):
456 self.check_run_submodule('absolute')
475 self.check_run_submodule('absolute')
457
476
458 def test_run_submodule_with_relative_import(self):
477 def test_run_submodule_with_relative_import(self):
459 """Run submodule that has a relative import statement (#2727)."""
478 """Run submodule that has a relative import statement (#2727)."""
460 self.check_run_submodule('relative')
479 self.check_run_submodule('relative')
461
480
462 def test_prun_submodule_with_absolute_import(self):
481 def test_prun_submodule_with_absolute_import(self):
463 self.check_run_submodule('absolute', '-p')
482 self.check_run_submodule('absolute', '-p')
464
483
465 def test_prun_submodule_with_relative_import(self):
484 def test_prun_submodule_with_relative_import(self):
466 self.check_run_submodule('relative', '-p')
485 self.check_run_submodule('relative', '-p')
467
486
468 def with_fake_debugger(func):
487 def with_fake_debugger(func):
469 @functools.wraps(func)
488 @functools.wraps(func)
470 def wrapper(*args, **kwds):
489 def wrapper(*args, **kwds):
471 with patch.object(debugger.Pdb, 'run', staticmethod(eval)):
490 with patch.object(debugger.Pdb, 'run', staticmethod(eval)):
472 return func(*args, **kwds)
491 return func(*args, **kwds)
473 return wrapper
492 return wrapper
474
493
475 @with_fake_debugger
494 @with_fake_debugger
476 def test_debug_run_submodule_with_absolute_import(self):
495 def test_debug_run_submodule_with_absolute_import(self):
477 self.check_run_submodule('absolute', '-d')
496 self.check_run_submodule('absolute', '-d')
478
497
479 @with_fake_debugger
498 @with_fake_debugger
480 def test_debug_run_submodule_with_relative_import(self):
499 def test_debug_run_submodule_with_relative_import(self):
481 self.check_run_submodule('relative', '-d')
500 self.check_run_submodule('relative', '-d')
482
501
483 def test_module_options(self):
502 def test_module_options(self):
484 _ip.user_ns.pop('a', None)
503 _ip.user_ns.pop('a', None)
485 test_opts = '-x abc -m test'
504 test_opts = '-x abc -m test'
486 _ip.run_line_magic('run', '-m {0}.args {1}'.format(self.package, test_opts))
505 _ip.run_line_magic('run', '-m {0}.args {1}'.format(self.package, test_opts))
487 nt.assert_equal(_ip.user_ns['a'], test_opts)
506 nt.assert_equal(_ip.user_ns['a'], test_opts)
488
507
489 def test_module_options_with_separator(self):
508 def test_module_options_with_separator(self):
490 _ip.user_ns.pop('a', None)
509 _ip.user_ns.pop('a', None)
491 test_opts = '-x abc -m test'
510 test_opts = '-x abc -m test'
492 _ip.run_line_magic('run', '-m {0}.args -- {1}'.format(self.package, test_opts))
511 _ip.run_line_magic('run', '-m {0}.args -- {1}'.format(self.package, test_opts))
493 nt.assert_equal(_ip.user_ns['a'], test_opts)
512 nt.assert_equal(_ip.user_ns['a'], test_opts)
494
513
495 def test_run__name__():
514 def test_run__name__():
496 with TemporaryDirectory() as td:
515 with TemporaryDirectory() as td:
497 path = pjoin(td, 'foo.py')
516 path = pjoin(td, 'foo.py')
498 with open(path, 'w') as f:
517 with open(path, 'w') as f:
499 f.write("q = __name__")
518 f.write("q = __name__")
500
519
501 _ip.user_ns.pop('q', None)
520 _ip.user_ns.pop('q', None)
502 _ip.magic('run {}'.format(path))
521 _ip.magic('run {}'.format(path))
503 nt.assert_equal(_ip.user_ns.pop('q'), '__main__')
522 nt.assert_equal(_ip.user_ns.pop('q'), '__main__')
504
523
505 _ip.magic('run -n {}'.format(path))
524 _ip.magic('run -n {}'.format(path))
506 nt.assert_equal(_ip.user_ns.pop('q'), 'foo')
525 nt.assert_equal(_ip.user_ns.pop('q'), 'foo')
507
526
508 try:
527 try:
509 _ip.magic('run -i -n {}'.format(path))
528 _ip.magic('run -i -n {}'.format(path))
510 nt.assert_equal(_ip.user_ns.pop('q'), 'foo')
529 nt.assert_equal(_ip.user_ns.pop('q'), 'foo')
511 finally:
530 finally:
512 _ip.magic('reset -f')
531 _ip.magic('reset -f')
513
532
514
533
515 def test_run_tb():
534 def test_run_tb():
516 """Test traceback offset in %run"""
535 """Test traceback offset in %run"""
517 with TemporaryDirectory() as td:
536 with TemporaryDirectory() as td:
518 path = pjoin(td, 'foo.py')
537 path = pjoin(td, 'foo.py')
519 with open(path, 'w') as f:
538 with open(path, 'w') as f:
520 f.write('\n'.join([
539 f.write('\n'.join([
521 "def foo():",
540 "def foo():",
522 " return bar()",
541 " return bar()",
523 "def bar():",
542 "def bar():",
524 " raise RuntimeError('hello!')",
543 " raise RuntimeError('hello!')",
525 "foo()",
544 "foo()",
526 ]))
545 ]))
527 with capture_output() as io:
546 with capture_output() as io:
528 _ip.magic('run {}'.format(path))
547 _ip.magic('run {}'.format(path))
529 out = io.stdout
548 out = io.stdout
530 nt.assert_not_in("execfile", out)
549 nt.assert_not_in("execfile", out)
531 nt.assert_in("RuntimeError", out)
550 nt.assert_in("RuntimeError", out)
532 nt.assert_equal(out.count("---->"), 3)
551 nt.assert_equal(out.count("---->"), 3)
533 del ip.user_ns['bar']
552 del ip.user_ns['bar']
534 del ip.user_ns['foo']
553 del ip.user_ns['foo']
535
554
536
555
537 def test_multiprocessing_run():
556 def test_multiprocessing_run():
538 """Set we can run mutiprocesgin without messing up up main namespace
557 """Set we can run mutiprocesgin without messing up up main namespace
539
558
540 Note that import `nose.tools as nt` mdify the value s
559 Note that import `nose.tools as nt` mdify the value s
541 sys.module['__mp_main__'] so wee need to temporarily set it to None to test
560 sys.module['__mp_main__'] so wee need to temporarily set it to None to test
542 the issue.
561 the issue.
543 """
562 """
544 with TemporaryDirectory() as td:
563 with TemporaryDirectory() as td:
545 mpm = sys.modules.get('__mp_main__')
564 mpm = sys.modules.get('__mp_main__')
546 assert mpm is not None
565 assert mpm is not None
547 sys.modules['__mp_main__'] = None
566 sys.modules['__mp_main__'] = None
548 try:
567 try:
549 path = pjoin(td, 'test.py')
568 path = pjoin(td, 'test.py')
550 with open(path, 'w') as f:
569 with open(path, 'w') as f:
551 f.write("import multiprocessing\nprint('hoy')")
570 f.write("import multiprocessing\nprint('hoy')")
552 with capture_output() as io:
571 with capture_output() as io:
553 _ip.run_line_magic('run', path)
572 _ip.run_line_magic('run', path)
554 _ip.run_cell("i_m_undefined")
573 _ip.run_cell("i_m_undefined")
555 out = io.stdout
574 out = io.stdout
556 nt.assert_in("hoy", out)
575 nt.assert_in("hoy", out)
557 nt.assert_not_in("AttributeError", out)
576 nt.assert_not_in("AttributeError", out)
558 nt.assert_in("NameError", out)
577 nt.assert_in("NameError", out)
559 nt.assert_equal(out.count("---->"), 1)
578 nt.assert_equal(out.count("---->"), 1)
560 except:
579 except:
561 raise
580 raise
562 finally:
581 finally:
563 sys.modules['__mp_main__'] = mpm
582 sys.modules['__mp_main__'] = mpm
564
583
565 @dec.knownfailureif(sys.platform == 'win32', "writes to io.stdout aren't captured on Windows")
584 @dec.knownfailureif(sys.platform == 'win32', "writes to io.stdout aren't captured on Windows")
566 def test_script_tb():
585 def test_script_tb():
567 """Test traceback offset in `ipython script.py`"""
586 """Test traceback offset in `ipython script.py`"""
568 with TemporaryDirectory() as td:
587 with TemporaryDirectory() as td:
569 path = pjoin(td, 'foo.py')
588 path = pjoin(td, 'foo.py')
570 with open(path, 'w') as f:
589 with open(path, 'w') as f:
571 f.write('\n'.join([
590 f.write('\n'.join([
572 "def foo():",
591 "def foo():",
573 " return bar()",
592 " return bar()",
574 "def bar():",
593 "def bar():",
575 " raise RuntimeError('hello!')",
594 " raise RuntimeError('hello!')",
576 "foo()",
595 "foo()",
577 ]))
596 ]))
578 out, err = tt.ipexec(path)
597 out, err = tt.ipexec(path)
579 nt.assert_not_in("execfile", out)
598 nt.assert_not_in("execfile", out)
580 nt.assert_in("RuntimeError", out)
599 nt.assert_in("RuntimeError", out)
581 nt.assert_equal(out.count("---->"), 3)
600 nt.assert_equal(out.count("---->"), 3)
582
601
General Comments 0
You need to be logged in to leave comments. Login now