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