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