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