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