##// END OF EJS Templates
Use +/- if stdout encoding does not support ±
Matthias Bussonnier -
Show More
@@ -1,1378 +1,1386 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
8 8 import ast
9 9 import bdb
10 10 import builtins as builtin_mod
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 import math
18 18 from pdb import Restart
19 19
20 20 # cProfile was added in Python2.5
21 21 try:
22 22 import cProfile as profile
23 23 import pstats
24 24 except ImportError:
25 25 # profile isn't bundled by default in Debian for license reasons
26 26 try:
27 27 import profile, pstats
28 28 except ImportError:
29 29 profile = pstats = None
30 30
31 31 from IPython.core import oinspect
32 32 from IPython.core import magic_arguments
33 33 from IPython.core import page
34 34 from IPython.core.error import UsageError
35 35 from IPython.core.macro import Macro
36 36 from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic,
37 37 line_cell_magic, on_off, needs_local_scope)
38 38 from IPython.testing.skipdoctest import skip_doctest
39 39 from IPython.utils.contexts import preserve_keys
40 40 from IPython.utils.capture import capture_output
41 41 from IPython.utils.ipstruct import Struct
42 42 from IPython.utils.module_paths import find_mod
43 43 from IPython.utils.path import get_py_filename, shellglob
44 44 from IPython.utils.timing import clock, clock2
45 45 from warnings import warn
46 46 from logging import error
47 47 from io import StringIO
48 48
49 49
50 50 #-----------------------------------------------------------------------------
51 51 # Magic implementation classes
52 52 #-----------------------------------------------------------------------------
53 53
54 54
55 55 class TimeitResult(object):
56 56 """
57 57 Object returned by the timeit magic with info about the run.
58 58
59 59 Contains the following attributes :
60 60
61 61 loops: (int) number of loops done per measurement
62 62 repeat: (int) number of times the measurement has been repeated
63 63 best: (float) best execution time / number
64 64 all_runs: (list of float) execution time of each run (in s)
65 65 compile_time: (float) time of statement compilation (s)
66 66
67 67 """
68 68 def __init__(self, loops, repeat, best, worst, all_runs, compile_time, precision):
69 69 self.loops = loops
70 70 self.repeat = repeat
71 71 self.best = best
72 72 self.worst = worst
73 73 self.all_runs = all_runs
74 74 self.compile_time = compile_time
75 75 self._precision = precision
76 76 self.timings = [ dt / self.loops for dt in all_runs]
77 77
78 78 @property
79 79 def average(self):
80 80 return math.fsum(self.timings) / len(self.timings)
81 81
82 82 @property
83 83 def stdev(self):
84 84 mean = self.average
85 85 return (math.fsum([(x - mean) ** 2 for x in self.timings]) / len(self.timings)) ** 0.5
86 86
87 87 def __str__(self):
88 pm = '+-'
89 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
90 try:
91 u'\xb1'.encode(sys.stdout.encoding)
92 pm = u'\xb1'
93 except:
94 pass
88 95 return (
89 u"{mean} ± {std} per loop (mean ± std. dev. of {runs} run{run_plural}, {loops} loop{loop_plural} each)"
96 u"{mean} {pm} {std} per loop (mean {pm} std. dev. of {runs} run{run_plural}, {loops} loop{loop_plural} each)"
90 97 .format(
98 pm = pm,
91 99 runs = self.repeat,
92 100 loops = self.loops,
93 101 loop_plural = "" if self.loops == 1 else "s",
94 102 run_plural = "" if self.repeat == 1 else "s",
95 103 mean = _format_time(self.average, self._precision),
96 104 std = _format_time(self.stdev, self._precision))
97 105 )
98 106
99 107 def _repr_pretty_(self, p , cycle):
100 108 unic = self.__str__()
101 109 p.text(u'<TimeitResult : '+unic+u'>')
102 110
103 111
104 112
105 113 class TimeitTemplateFiller(ast.NodeTransformer):
106 114 """Fill in the AST template for timing execution.
107 115
108 116 This is quite closely tied to the template definition, which is in
109 117 :meth:`ExecutionMagics.timeit`.
110 118 """
111 119 def __init__(self, ast_setup, ast_stmt):
112 120 self.ast_setup = ast_setup
113 121 self.ast_stmt = ast_stmt
114 122
115 123 def visit_FunctionDef(self, node):
116 124 "Fill in the setup statement"
117 125 self.generic_visit(node)
118 126 if node.name == "inner":
119 127 node.body[:1] = self.ast_setup.body
120 128
121 129 return node
122 130
123 131 def visit_For(self, node):
124 132 "Fill in the statement to be timed"
125 133 if getattr(getattr(node.body[0], 'value', None), 'id', None) == 'stmt':
126 134 node.body = self.ast_stmt.body
127 135 return node
128 136
129 137
130 138 class Timer(timeit.Timer):
131 139 """Timer class that explicitly uses self.inner
132 140
133 141 which is an undocumented implementation detail of CPython,
134 142 not shared by PyPy.
135 143 """
136 144 # Timer.timeit copied from CPython 3.4.2
137 145 def timeit(self, number=timeit.default_number):
138 146 """Time 'number' executions of the main statement.
139 147
140 148 To be precise, this executes the setup statement once, and
141 149 then returns the time it takes to execute the main statement
142 150 a number of times, as a float measured in seconds. The
143 151 argument is the number of times through the loop, defaulting
144 152 to one million. The main statement, the setup statement and
145 153 the timer function to be used are passed to the constructor.
146 154 """
147 155 it = itertools.repeat(None, number)
148 156 gcold = gc.isenabled()
149 157 gc.disable()
150 158 try:
151 159 timing = self.inner(it, self.timer)
152 160 finally:
153 161 if gcold:
154 162 gc.enable()
155 163 return timing
156 164
157 165
158 166 @magics_class
159 167 class ExecutionMagics(Magics):
160 168 """Magics related to code execution, debugging, profiling, etc.
161 169
162 170 """
163 171
164 172 def __init__(self, shell):
165 173 super(ExecutionMagics, self).__init__(shell)
166 174 if profile is None:
167 175 self.prun = self.profile_missing_notice
168 176 # Default execution function used to actually run user code.
169 177 self.default_runner = None
170 178
171 179 def profile_missing_notice(self, *args, **kwargs):
172 180 error("""\
173 181 The profile module could not be found. It has been removed from the standard
174 182 python packages because of its non-free license. To use profiling, install the
175 183 python-profiler package from non-free.""")
176 184
177 185 @skip_doctest
178 186 @line_cell_magic
179 187 def prun(self, parameter_s='', cell=None):
180 188
181 189 """Run a statement through the python code profiler.
182 190
183 191 Usage, in line mode:
184 192 %prun [options] statement
185 193
186 194 Usage, in cell mode:
187 195 %%prun [options] [statement]
188 196 code...
189 197 code...
190 198
191 199 In cell mode, the additional code lines are appended to the (possibly
192 200 empty) statement in the first line. Cell mode allows you to easily
193 201 profile multiline blocks without having to put them in a separate
194 202 function.
195 203
196 204 The given statement (which doesn't require quote marks) is run via the
197 205 python profiler in a manner similar to the profile.run() function.
198 206 Namespaces are internally managed to work correctly; profile.run
199 207 cannot be used in IPython because it makes certain assumptions about
200 208 namespaces which do not hold under IPython.
201 209
202 210 Options:
203 211
204 212 -l <limit>
205 213 you can place restrictions on what or how much of the
206 214 profile gets printed. The limit value can be:
207 215
208 216 * A string: only information for function names containing this string
209 217 is printed.
210 218
211 219 * An integer: only these many lines are printed.
212 220
213 221 * A float (between 0 and 1): this fraction of the report is printed
214 222 (for example, use a limit of 0.4 to see the topmost 40% only).
215 223
216 224 You can combine several limits with repeated use of the option. For
217 225 example, ``-l __init__ -l 5`` will print only the topmost 5 lines of
218 226 information about class constructors.
219 227
220 228 -r
221 229 return the pstats.Stats object generated by the profiling. This
222 230 object has all the information about the profile in it, and you can
223 231 later use it for further analysis or in other functions.
224 232
225 233 -s <key>
226 234 sort profile by given key. You can provide more than one key
227 235 by using the option several times: '-s key1 -s key2 -s key3...'. The
228 236 default sorting key is 'time'.
229 237
230 238 The following is copied verbatim from the profile documentation
231 239 referenced below:
232 240
233 241 When more than one key is provided, additional keys are used as
234 242 secondary criteria when the there is equality in all keys selected
235 243 before them.
236 244
237 245 Abbreviations can be used for any key names, as long as the
238 246 abbreviation is unambiguous. The following are the keys currently
239 247 defined:
240 248
241 249 ============ =====================
242 250 Valid Arg Meaning
243 251 ============ =====================
244 252 "calls" call count
245 253 "cumulative" cumulative time
246 254 "file" file name
247 255 "module" file name
248 256 "pcalls" primitive call count
249 257 "line" line number
250 258 "name" function name
251 259 "nfl" name/file/line
252 260 "stdname" standard name
253 261 "time" internal time
254 262 ============ =====================
255 263
256 264 Note that all sorts on statistics are in descending order (placing
257 265 most time consuming items first), where as name, file, and line number
258 266 searches are in ascending order (i.e., alphabetical). The subtle
259 267 distinction between "nfl" and "stdname" is that the standard name is a
260 268 sort of the name as printed, which means that the embedded line
261 269 numbers get compared in an odd way. For example, lines 3, 20, and 40
262 270 would (if the file names were the same) appear in the string order
263 271 "20" "3" and "40". In contrast, "nfl" does a numeric compare of the
264 272 line numbers. In fact, sort_stats("nfl") is the same as
265 273 sort_stats("name", "file", "line").
266 274
267 275 -T <filename>
268 276 save profile results as shown on screen to a text
269 277 file. The profile is still shown on screen.
270 278
271 279 -D <filename>
272 280 save (via dump_stats) profile statistics to given
273 281 filename. This data is in a format understood by the pstats module, and
274 282 is generated by a call to the dump_stats() method of profile
275 283 objects. The profile is still shown on screen.
276 284
277 285 -q
278 286 suppress output to the pager. Best used with -T and/or -D above.
279 287
280 288 If you want to run complete programs under the profiler's control, use
281 289 ``%run -p [prof_opts] filename.py [args to program]`` where prof_opts
282 290 contains profiler specific options as described here.
283 291
284 292 You can read the complete documentation for the profile module with::
285 293
286 294 In [1]: import profile; profile.help()
287 295 """
288 296 opts, arg_str = self.parse_options(parameter_s, 'D:l:rs:T:q',
289 297 list_all=True, posix=False)
290 298 if cell is not None:
291 299 arg_str += '\n' + cell
292 300 arg_str = self.shell.input_splitter.transform_cell(arg_str)
293 301 return self._run_with_profiler(arg_str, opts, self.shell.user_ns)
294 302
295 303 def _run_with_profiler(self, code, opts, namespace):
296 304 """
297 305 Run `code` with profiler. Used by ``%prun`` and ``%run -p``.
298 306
299 307 Parameters
300 308 ----------
301 309 code : str
302 310 Code to be executed.
303 311 opts : Struct
304 312 Options parsed by `self.parse_options`.
305 313 namespace : dict
306 314 A dictionary for Python namespace (e.g., `self.shell.user_ns`).
307 315
308 316 """
309 317
310 318 # Fill default values for unspecified options:
311 319 opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))
312 320
313 321 prof = profile.Profile()
314 322 try:
315 323 prof = prof.runctx(code, namespace, namespace)
316 324 sys_exit = ''
317 325 except SystemExit:
318 326 sys_exit = """*** SystemExit exception caught in code being profiled."""
319 327
320 328 stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)
321 329
322 330 lims = opts.l
323 331 if lims:
324 332 lims = [] # rebuild lims with ints/floats/strings
325 333 for lim in opts.l:
326 334 try:
327 335 lims.append(int(lim))
328 336 except ValueError:
329 337 try:
330 338 lims.append(float(lim))
331 339 except ValueError:
332 340 lims.append(lim)
333 341
334 342 # Trap output.
335 343 stdout_trap = StringIO()
336 344 stats_stream = stats.stream
337 345 try:
338 346 stats.stream = stdout_trap
339 347 stats.print_stats(*lims)
340 348 finally:
341 349 stats.stream = stats_stream
342 350
343 351 output = stdout_trap.getvalue()
344 352 output = output.rstrip()
345 353
346 354 if 'q' not in opts:
347 355 page.page(output)
348 356 print(sys_exit, end=' ')
349 357
350 358 dump_file = opts.D[0]
351 359 text_file = opts.T[0]
352 360 if dump_file:
353 361 prof.dump_stats(dump_file)
354 362 print('\n*** Profile stats marshalled to file',\
355 363 repr(dump_file)+'.',sys_exit)
356 364 if text_file:
357 365 pfile = open(text_file,'w')
358 366 pfile.write(output)
359 367 pfile.close()
360 368 print('\n*** Profile printout saved to text file',\
361 369 repr(text_file)+'.',sys_exit)
362 370
363 371 if 'r' in opts:
364 372 return stats
365 373 else:
366 374 return None
367 375
368 376 @line_magic
369 377 def pdb(self, parameter_s=''):
370 378 """Control the automatic calling of the pdb interactive debugger.
371 379
372 380 Call as '%pdb on', '%pdb 1', '%pdb off' or '%pdb 0'. If called without
373 381 argument it works as a toggle.
374 382
375 383 When an exception is triggered, IPython can optionally call the
376 384 interactive pdb debugger after the traceback printout. %pdb toggles
377 385 this feature on and off.
378 386
379 387 The initial state of this feature is set in your configuration
380 388 file (the option is ``InteractiveShell.pdb``).
381 389
382 390 If you want to just activate the debugger AFTER an exception has fired,
383 391 without having to type '%pdb on' and rerunning your code, you can use
384 392 the %debug magic."""
385 393
386 394 par = parameter_s.strip().lower()
387 395
388 396 if par:
389 397 try:
390 398 new_pdb = {'off':0,'0':0,'on':1,'1':1}[par]
391 399 except KeyError:
392 400 print ('Incorrect argument. Use on/1, off/0, '
393 401 'or nothing for a toggle.')
394 402 return
395 403 else:
396 404 # toggle
397 405 new_pdb = not self.shell.call_pdb
398 406
399 407 # set on the shell
400 408 self.shell.call_pdb = new_pdb
401 409 print('Automatic pdb calling has been turned',on_off(new_pdb))
402 410
403 411 @skip_doctest
404 412 @magic_arguments.magic_arguments()
405 413 @magic_arguments.argument('--breakpoint', '-b', metavar='FILE:LINE',
406 414 help="""
407 415 Set break point at LINE in FILE.
408 416 """
409 417 )
410 418 @magic_arguments.argument('statement', nargs='*',
411 419 help="""
412 420 Code to run in debugger.
413 421 You can omit this in cell magic mode.
414 422 """
415 423 )
416 424 @line_cell_magic
417 425 def debug(self, line='', cell=None):
418 426 """Activate the interactive debugger.
419 427
420 428 This magic command support two ways of activating debugger.
421 429 One is to activate debugger before executing code. This way, you
422 430 can set a break point, to step through the code from the point.
423 431 You can use this mode by giving statements to execute and optionally
424 432 a breakpoint.
425 433
426 434 The other one is to activate debugger in post-mortem mode. You can
427 435 activate this mode simply running %debug without any argument.
428 436 If an exception has just occurred, this lets you inspect its stack
429 437 frames interactively. Note that this will always work only on the last
430 438 traceback that occurred, so you must call this quickly after an
431 439 exception that you wish to inspect has fired, because if another one
432 440 occurs, it clobbers the previous one.
433 441
434 442 If you want IPython to automatically do this on every exception, see
435 443 the %pdb magic for more details.
436 444 """
437 445 args = magic_arguments.parse_argstring(self.debug, line)
438 446
439 447 if not (args.breakpoint or args.statement or cell):
440 448 self._debug_post_mortem()
441 449 else:
442 450 code = "\n".join(args.statement)
443 451 if cell:
444 452 code += "\n" + cell
445 453 self._debug_exec(code, args.breakpoint)
446 454
447 455 def _debug_post_mortem(self):
448 456 self.shell.debugger(force=True)
449 457
450 458 def _debug_exec(self, code, breakpoint):
451 459 if breakpoint:
452 460 (filename, bp_line) = breakpoint.rsplit(':', 1)
453 461 bp_line = int(bp_line)
454 462 else:
455 463 (filename, bp_line) = (None, None)
456 464 self._run_with_debugger(code, self.shell.user_ns, filename, bp_line)
457 465
458 466 @line_magic
459 467 def tb(self, s):
460 468 """Print the last traceback with the currently active exception mode.
461 469
462 470 See %xmode for changing exception reporting modes."""
463 471 self.shell.showtraceback()
464 472
465 473 @skip_doctest
466 474 @line_magic
467 475 def run(self, parameter_s='', runner=None,
468 476 file_finder=get_py_filename):
469 477 """Run the named file inside IPython as a program.
470 478
471 479 Usage::
472 480
473 481 %run [-n -i -e -G]
474 482 [( -t [-N<N>] | -d [-b<N>] | -p [profile options] )]
475 483 ( -m mod | file ) [args]
476 484
477 485 Parameters after the filename are passed as command-line arguments to
478 486 the program (put in sys.argv). Then, control returns to IPython's
479 487 prompt.
480 488
481 489 This is similar to running at a system prompt ``python file args``,
482 490 but with the advantage of giving you IPython's tracebacks, and of
483 491 loading all variables into your interactive namespace for further use
484 492 (unless -p is used, see below).
485 493
486 494 The file is executed in a namespace initially consisting only of
487 495 ``__name__=='__main__'`` and sys.argv constructed as indicated. It thus
488 496 sees its environment as if it were being run as a stand-alone program
489 497 (except for sharing global objects such as previously imported
490 498 modules). But after execution, the IPython interactive namespace gets
491 499 updated with all variables defined in the program (except for __name__
492 500 and sys.argv). This allows for very convenient loading of code for
493 501 interactive work, while giving each program a 'clean sheet' to run in.
494 502
495 503 Arguments are expanded using shell-like glob match. Patterns
496 504 '*', '?', '[seq]' and '[!seq]' can be used. Additionally,
497 505 tilde '~' will be expanded into user's home directory. Unlike
498 506 real shells, quotation does not suppress expansions. Use
499 507 *two* back slashes (e.g. ``\\\\*``) to suppress expansions.
500 508 To completely disable these expansions, you can use -G flag.
501 509
502 510 Options:
503 511
504 512 -n
505 513 __name__ is NOT set to '__main__', but to the running file's name
506 514 without extension (as python does under import). This allows running
507 515 scripts and reloading the definitions in them without calling code
508 516 protected by an ``if __name__ == "__main__"`` clause.
509 517
510 518 -i
511 519 run the file in IPython's namespace instead of an empty one. This
512 520 is useful if you are experimenting with code written in a text editor
513 521 which depends on variables defined interactively.
514 522
515 523 -e
516 524 ignore sys.exit() calls or SystemExit exceptions in the script
517 525 being run. This is particularly useful if IPython is being used to
518 526 run unittests, which always exit with a sys.exit() call. In such
519 527 cases you are interested in the output of the test results, not in
520 528 seeing a traceback of the unittest module.
521 529
522 530 -t
523 531 print timing information at the end of the run. IPython will give
524 532 you an estimated CPU time consumption for your script, which under
525 533 Unix uses the resource module to avoid the wraparound problems of
526 534 time.clock(). Under Unix, an estimate of time spent on system tasks
527 535 is also given (for Windows platforms this is reported as 0.0).
528 536
529 537 If -t is given, an additional ``-N<N>`` option can be given, where <N>
530 538 must be an integer indicating how many times you want the script to
531 539 run. The final timing report will include total and per run results.
532 540
533 541 For example (testing the script uniq_stable.py)::
534 542
535 543 In [1]: run -t uniq_stable
536 544
537 545 IPython CPU timings (estimated):
538 546 User : 0.19597 s.
539 547 System: 0.0 s.
540 548
541 549 In [2]: run -t -N5 uniq_stable
542 550
543 551 IPython CPU timings (estimated):
544 552 Total runs performed: 5
545 553 Times : Total Per run
546 554 User : 0.910862 s, 0.1821724 s.
547 555 System: 0.0 s, 0.0 s.
548 556
549 557 -d
550 558 run your program under the control of pdb, the Python debugger.
551 559 This allows you to execute your program step by step, watch variables,
552 560 etc. Internally, what IPython does is similar to calling::
553 561
554 562 pdb.run('execfile("YOURFILENAME")')
555 563
556 564 with a breakpoint set on line 1 of your file. You can change the line
557 565 number for this automatic breakpoint to be <N> by using the -bN option
558 566 (where N must be an integer). For example::
559 567
560 568 %run -d -b40 myscript
561 569
562 570 will set the first breakpoint at line 40 in myscript.py. Note that
563 571 the first breakpoint must be set on a line which actually does
564 572 something (not a comment or docstring) for it to stop execution.
565 573
566 574 Or you can specify a breakpoint in a different file::
567 575
568 576 %run -d -b myotherfile.py:20 myscript
569 577
570 578 When the pdb debugger starts, you will see a (Pdb) prompt. You must
571 579 first enter 'c' (without quotes) to start execution up to the first
572 580 breakpoint.
573 581
574 582 Entering 'help' gives information about the use of the debugger. You
575 583 can easily see pdb's full documentation with "import pdb;pdb.help()"
576 584 at a prompt.
577 585
578 586 -p
579 587 run program under the control of the Python profiler module (which
580 588 prints a detailed report of execution times, function calls, etc).
581 589
582 590 You can pass other options after -p which affect the behavior of the
583 591 profiler itself. See the docs for %prun for details.
584 592
585 593 In this mode, the program's variables do NOT propagate back to the
586 594 IPython interactive namespace (because they remain in the namespace
587 595 where the profiler executes them).
588 596
589 597 Internally this triggers a call to %prun, see its documentation for
590 598 details on the options available specifically for profiling.
591 599
592 600 There is one special usage for which the text above doesn't apply:
593 601 if the filename ends with .ipy[nb], the file is run as ipython script,
594 602 just as if the commands were written on IPython prompt.
595 603
596 604 -m
597 605 specify module name to load instead of script path. Similar to
598 606 the -m option for the python interpreter. Use this option last if you
599 607 want to combine with other %run options. Unlike the python interpreter
600 608 only source modules are allowed no .pyc or .pyo files.
601 609 For example::
602 610
603 611 %run -m example
604 612
605 613 will run the example module.
606 614
607 615 -G
608 616 disable shell-like glob expansion of arguments.
609 617
610 618 """
611 619
612 620 # get arguments and set sys.argv for program to be run.
613 621 opts, arg_lst = self.parse_options(parameter_s,
614 622 'nidtN:b:pD:l:rs:T:em:G',
615 623 mode='list', list_all=1)
616 624 if "m" in opts:
617 625 modulename = opts["m"][0]
618 626 modpath = find_mod(modulename)
619 627 if modpath is None:
620 628 warn('%r is not a valid modulename on sys.path'%modulename)
621 629 return
622 630 arg_lst = [modpath] + arg_lst
623 631 try:
624 632 filename = file_finder(arg_lst[0])
625 633 except IndexError:
626 634 warn('you must provide at least a filename.')
627 635 print('\n%run:\n', oinspect.getdoc(self.run))
628 636 return
629 637 except IOError as e:
630 638 try:
631 639 msg = str(e)
632 640 except UnicodeError:
633 641 msg = e.message
634 642 error(msg)
635 643 return
636 644
637 645 if filename.lower().endswith(('.ipy', '.ipynb')):
638 646 with preserve_keys(self.shell.user_ns, '__file__'):
639 647 self.shell.user_ns['__file__'] = filename
640 648 self.shell.safe_execfile_ipy(filename)
641 649 return
642 650
643 651 # Control the response to exit() calls made by the script being run
644 652 exit_ignore = 'e' in opts
645 653
646 654 # Make sure that the running script gets a proper sys.argv as if it
647 655 # were run from a system shell.
648 656 save_argv = sys.argv # save it for later restoring
649 657
650 658 if 'G' in opts:
651 659 args = arg_lst[1:]
652 660 else:
653 661 # tilde and glob expansion
654 662 args = shellglob(map(os.path.expanduser, arg_lst[1:]))
655 663
656 664 sys.argv = [filename] + args # put in the proper filename
657 665
658 666 if 'n' in opts:
659 667 name = os.path.splitext(os.path.basename(filename))[0]
660 668 else:
661 669 name = '__main__'
662 670
663 671 if 'i' in opts:
664 672 # Run in user's interactive namespace
665 673 prog_ns = self.shell.user_ns
666 674 __name__save = self.shell.user_ns['__name__']
667 675 prog_ns['__name__'] = name
668 676 main_mod = self.shell.user_module
669 677
670 678 # Since '%run foo' emulates 'python foo.py' at the cmd line, we must
671 679 # set the __file__ global in the script's namespace
672 680 # TK: Is this necessary in interactive mode?
673 681 prog_ns['__file__'] = filename
674 682 else:
675 683 # Run in a fresh, empty namespace
676 684
677 685 # The shell MUST hold a reference to prog_ns so after %run
678 686 # exits, the python deletion mechanism doesn't zero it out
679 687 # (leaving dangling references). See interactiveshell for details
680 688 main_mod = self.shell.new_main_mod(filename, name)
681 689 prog_ns = main_mod.__dict__
682 690
683 691 # pickle fix. See interactiveshell for an explanation. But we need to
684 692 # make sure that, if we overwrite __main__, we replace it at the end
685 693 main_mod_name = prog_ns['__name__']
686 694
687 695 if main_mod_name == '__main__':
688 696 restore_main = sys.modules['__main__']
689 697 else:
690 698 restore_main = False
691 699
692 700 # This needs to be undone at the end to prevent holding references to
693 701 # every single object ever created.
694 702 sys.modules[main_mod_name] = main_mod
695 703
696 704 if 'p' in opts or 'd' in opts:
697 705 if 'm' in opts:
698 706 code = 'run_module(modulename, prog_ns)'
699 707 code_ns = {
700 708 'run_module': self.shell.safe_run_module,
701 709 'prog_ns': prog_ns,
702 710 'modulename': modulename,
703 711 }
704 712 else:
705 713 if 'd' in opts:
706 714 # allow exceptions to raise in debug mode
707 715 code = 'execfile(filename, prog_ns, raise_exceptions=True)'
708 716 else:
709 717 code = 'execfile(filename, prog_ns)'
710 718 code_ns = {
711 719 'execfile': self.shell.safe_execfile,
712 720 'prog_ns': prog_ns,
713 721 'filename': get_py_filename(filename),
714 722 }
715 723
716 724 try:
717 725 stats = None
718 726 if 'p' in opts:
719 727 stats = self._run_with_profiler(code, opts, code_ns)
720 728 else:
721 729 if 'd' in opts:
722 730 bp_file, bp_line = parse_breakpoint(
723 731 opts.get('b', ['1'])[0], filename)
724 732 self._run_with_debugger(
725 733 code, code_ns, filename, bp_line, bp_file)
726 734 else:
727 735 if 'm' in opts:
728 736 def run():
729 737 self.shell.safe_run_module(modulename, prog_ns)
730 738 else:
731 739 if runner is None:
732 740 runner = self.default_runner
733 741 if runner is None:
734 742 runner = self.shell.safe_execfile
735 743
736 744 def run():
737 745 runner(filename, prog_ns, prog_ns,
738 746 exit_ignore=exit_ignore)
739 747
740 748 if 't' in opts:
741 749 # timed execution
742 750 try:
743 751 nruns = int(opts['N'][0])
744 752 if nruns < 1:
745 753 error('Number of runs must be >=1')
746 754 return
747 755 except (KeyError):
748 756 nruns = 1
749 757 self._run_with_timing(run, nruns)
750 758 else:
751 759 # regular execution
752 760 run()
753 761
754 762 if 'i' in opts:
755 763 self.shell.user_ns['__name__'] = __name__save
756 764 else:
757 765 # update IPython interactive namespace
758 766
759 767 # Some forms of read errors on the file may mean the
760 768 # __name__ key was never set; using pop we don't have to
761 769 # worry about a possible KeyError.
762 770 prog_ns.pop('__name__', None)
763 771
764 772 with preserve_keys(self.shell.user_ns, '__file__'):
765 773 self.shell.user_ns.update(prog_ns)
766 774 finally:
767 775 # It's a bit of a mystery why, but __builtins__ can change from
768 776 # being a module to becoming a dict missing some key data after
769 777 # %run. As best I can see, this is NOT something IPython is doing
770 778 # at all, and similar problems have been reported before:
771 779 # http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-10/0188.html
772 780 # Since this seems to be done by the interpreter itself, the best
773 781 # we can do is to at least restore __builtins__ for the user on
774 782 # exit.
775 783 self.shell.user_ns['__builtins__'] = builtin_mod
776 784
777 785 # Ensure key global structures are restored
778 786 sys.argv = save_argv
779 787 if restore_main:
780 788 sys.modules['__main__'] = restore_main
781 789 else:
782 790 # Remove from sys.modules the reference to main_mod we'd
783 791 # added. Otherwise it will trap references to objects
784 792 # contained therein.
785 793 del sys.modules[main_mod_name]
786 794
787 795 return stats
788 796
789 797 def _run_with_debugger(self, code, code_ns, filename=None,
790 798 bp_line=None, bp_file=None):
791 799 """
792 800 Run `code` in debugger with a break point.
793 801
794 802 Parameters
795 803 ----------
796 804 code : str
797 805 Code to execute.
798 806 code_ns : dict
799 807 A namespace in which `code` is executed.
800 808 filename : str
801 809 `code` is ran as if it is in `filename`.
802 810 bp_line : int, optional
803 811 Line number of the break point.
804 812 bp_file : str, optional
805 813 Path to the file in which break point is specified.
806 814 `filename` is used if not given.
807 815
808 816 Raises
809 817 ------
810 818 UsageError
811 819 If the break point given by `bp_line` is not valid.
812 820
813 821 """
814 822 deb = self.shell.InteractiveTB.pdb
815 823 if not deb:
816 824 self.shell.InteractiveTB.pdb = self.shell.InteractiveTB.debugger_cls()
817 825 deb = self.shell.InteractiveTB.pdb
818 826
819 827 # deb.checkline() fails if deb.curframe exists but is None; it can
820 828 # handle it not existing. https://github.com/ipython/ipython/issues/10028
821 829 if hasattr(deb, 'curframe'):
822 830 del deb.curframe
823 831
824 832 # reset Breakpoint state, which is moronically kept
825 833 # in a class
826 834 bdb.Breakpoint.next = 1
827 835 bdb.Breakpoint.bplist = {}
828 836 bdb.Breakpoint.bpbynumber = [None]
829 837 if bp_line is not None:
830 838 # Set an initial breakpoint to stop execution
831 839 maxtries = 10
832 840 bp_file = bp_file or filename
833 841 checkline = deb.checkline(bp_file, bp_line)
834 842 if not checkline:
835 843 for bp in range(bp_line + 1, bp_line + maxtries + 1):
836 844 if deb.checkline(bp_file, bp):
837 845 break
838 846 else:
839 847 msg = ("\nI failed to find a valid line to set "
840 848 "a breakpoint\n"
841 849 "after trying up to line: %s.\n"
842 850 "Please set a valid breakpoint manually "
843 851 "with the -b option." % bp)
844 852 raise UsageError(msg)
845 853 # if we find a good linenumber, set the breakpoint
846 854 deb.do_break('%s:%s' % (bp_file, bp_line))
847 855
848 856 if filename:
849 857 # Mimic Pdb._runscript(...)
850 858 deb._wait_for_mainpyfile = True
851 859 deb.mainpyfile = deb.canonic(filename)
852 860
853 861 # Start file run
854 862 print("NOTE: Enter 'c' at the %s prompt to continue execution." % deb.prompt)
855 863 try:
856 864 if filename:
857 865 # save filename so it can be used by methods on the deb object
858 866 deb._exec_filename = filename
859 867 while True:
860 868 try:
861 869 deb.run(code, code_ns)
862 870 except Restart:
863 871 print("Restarting")
864 872 if filename:
865 873 deb._wait_for_mainpyfile = True
866 874 deb.mainpyfile = deb.canonic(filename)
867 875 continue
868 876 else:
869 877 break
870 878
871 879
872 880 except:
873 881 etype, value, tb = sys.exc_info()
874 882 # Skip three frames in the traceback: the %run one,
875 883 # one inside bdb.py, and the command-line typed by the
876 884 # user (run by exec in pdb itself).
877 885 self.shell.InteractiveTB(etype, value, tb, tb_offset=3)
878 886
879 887 @staticmethod
880 888 def _run_with_timing(run, nruns):
881 889 """
882 890 Run function `run` and print timing information.
883 891
884 892 Parameters
885 893 ----------
886 894 run : callable
887 895 Any callable object which takes no argument.
888 896 nruns : int
889 897 Number of times to execute `run`.
890 898
891 899 """
892 900 twall0 = time.time()
893 901 if nruns == 1:
894 902 t0 = clock2()
895 903 run()
896 904 t1 = clock2()
897 905 t_usr = t1[0] - t0[0]
898 906 t_sys = t1[1] - t0[1]
899 907 print("\nIPython CPU timings (estimated):")
900 908 print(" User : %10.2f s." % t_usr)
901 909 print(" System : %10.2f s." % t_sys)
902 910 else:
903 911 runs = range(nruns)
904 912 t0 = clock2()
905 913 for nr in runs:
906 914 run()
907 915 t1 = clock2()
908 916 t_usr = t1[0] - t0[0]
909 917 t_sys = t1[1] - t0[1]
910 918 print("\nIPython CPU timings (estimated):")
911 919 print("Total runs performed:", nruns)
912 920 print(" Times : %10s %10s" % ('Total', 'Per run'))
913 921 print(" User : %10.2f s, %10.2f s." % (t_usr, t_usr / nruns))
914 922 print(" System : %10.2f s, %10.2f s." % (t_sys, t_sys / nruns))
915 923 twall1 = time.time()
916 924 print("Wall time: %10.2f s." % (twall1 - twall0))
917 925
918 926 @skip_doctest
919 927 @line_cell_magic
920 928 def timeit(self, line='', cell=None):
921 929 """Time execution of a Python statement or expression
922 930
923 931 Usage, in line mode:
924 932 %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
925 933 or in cell mode:
926 934 %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
927 935 code
928 936 code...
929 937
930 938 Time execution of a Python statement or expression using the timeit
931 939 module. This function can be used both as a line and cell magic:
932 940
933 941 - In line mode you can time a single-line statement (though multiple
934 942 ones can be chained with using semicolons).
935 943
936 944 - In cell mode, the statement in the first line is used as setup code
937 945 (executed but not timed) and the body of the cell is timed. The cell
938 946 body has access to any variables created in the setup code.
939 947
940 948 Options:
941 949 -n<N>: execute the given statement <N> times in a loop. If this value
942 950 is not given, a fitting value is chosen.
943 951
944 952 -r<R>: repeat the loop iteration <R> times and take the best result.
945 953 Default: 3
946 954
947 955 -t: use time.time to measure the time, which is the default on Unix.
948 956 This function measures wall time.
949 957
950 958 -c: use time.clock to measure the time, which is the default on
951 959 Windows and measures wall time. On Unix, resource.getrusage is used
952 960 instead and returns the CPU user time.
953 961
954 962 -p<P>: use a precision of <P> digits to display the timing result.
955 963 Default: 3
956 964
957 965 -q: Quiet, do not print result.
958 966
959 967 -o: return a TimeitResult that can be stored in a variable to inspect
960 968 the result in more details.
961 969
962 970
963 971 Examples
964 972 --------
965 973 ::
966 974
967 975 In [1]: %timeit pass
968 976 8.26 ns ± 0.12 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
969 977
970 978 In [2]: u = None
971 979
972 980 In [3]: %timeit u is None
973 981 29.9 ns ± 0.643 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
974 982
975 983 In [4]: %timeit -r 4 u == None
976 984
977 985 In [5]: import time
978 986
979 987 In [6]: %timeit -n1 time.sleep(2)
980 988
981 989
982 990 The times reported by %timeit will be slightly higher than those
983 991 reported by the timeit.py script when variables are accessed. This is
984 992 due to the fact that %timeit executes the statement in the namespace
985 993 of the shell, compared with timeit.py, which uses a single setup
986 994 statement to import function or create variables. Generally, the bias
987 995 does not matter as long as results from timeit.py are not mixed with
988 996 those from %timeit."""
989 997
990 998 opts, stmt = self.parse_options(line,'n:r:tcp:qo',
991 999 posix=False, strict=False)
992 1000 if stmt == "" and cell is None:
993 1001 return
994 1002
995 1003 timefunc = timeit.default_timer
996 1004 number = int(getattr(opts, "n", 0))
997 1005 default_repeat = 7 if timeit.default_repeat < 7 else timeit.default_repeat
998 1006 repeat = int(getattr(opts, "r", default_repeat))
999 1007 precision = int(getattr(opts, "p", 3))
1000 1008 quiet = 'q' in opts
1001 1009 return_result = 'o' in opts
1002 1010 if hasattr(opts, "t"):
1003 1011 timefunc = time.time
1004 1012 if hasattr(opts, "c"):
1005 1013 timefunc = clock
1006 1014
1007 1015 timer = Timer(timer=timefunc)
1008 1016 # this code has tight coupling to the inner workings of timeit.Timer,
1009 1017 # but is there a better way to achieve that the code stmt has access
1010 1018 # to the shell namespace?
1011 1019 transform = self.shell.input_splitter.transform_cell
1012 1020
1013 1021 if cell is None:
1014 1022 # called as line magic
1015 1023 ast_setup = self.shell.compile.ast_parse("pass")
1016 1024 ast_stmt = self.shell.compile.ast_parse(transform(stmt))
1017 1025 else:
1018 1026 ast_setup = self.shell.compile.ast_parse(transform(stmt))
1019 1027 ast_stmt = self.shell.compile.ast_parse(transform(cell))
1020 1028
1021 1029 ast_setup = self.shell.transform_ast(ast_setup)
1022 1030 ast_stmt = self.shell.transform_ast(ast_stmt)
1023 1031
1024 1032 # This codestring is taken from timeit.template - we fill it in as an
1025 1033 # AST, so that we can apply our AST transformations to the user code
1026 1034 # without affecting the timing code.
1027 1035 timeit_ast_template = ast.parse('def inner(_it, _timer):\n'
1028 1036 ' setup\n'
1029 1037 ' _t0 = _timer()\n'
1030 1038 ' for _i in _it:\n'
1031 1039 ' stmt\n'
1032 1040 ' _t1 = _timer()\n'
1033 1041 ' return _t1 - _t0\n')
1034 1042
1035 1043 timeit_ast = TimeitTemplateFiller(ast_setup, ast_stmt).visit(timeit_ast_template)
1036 1044 timeit_ast = ast.fix_missing_locations(timeit_ast)
1037 1045
1038 1046 # Track compilation time so it can be reported if too long
1039 1047 # Minimum time above which compilation time will be reported
1040 1048 tc_min = 0.1
1041 1049
1042 1050 t0 = clock()
1043 1051 code = self.shell.compile(timeit_ast, "<magic-timeit>", "exec")
1044 1052 tc = clock()-t0
1045 1053
1046 1054 ns = {}
1047 1055 exec(code, self.shell.user_ns, ns)
1048 1056 timer.inner = ns["inner"]
1049 1057
1050 1058 # This is used to check if there is a huge difference between the
1051 1059 # best and worst timings.
1052 1060 # Issue: https://github.com/ipython/ipython/issues/6471
1053 1061 if number == 0:
1054 1062 # determine number so that 0.2 <= total time < 2.0
1055 1063 for index in range(0, 10):
1056 1064 number = 10 ** index
1057 1065 time_number = timer.timeit(number)
1058 1066 if time_number >= 0.2:
1059 1067 break
1060 1068
1061 1069 all_runs = timer.repeat(repeat, number)
1062 1070 best = min(all_runs) / number
1063 1071 worst = max(all_runs) / number
1064 1072 timeit_result = TimeitResult(number, repeat, best, worst, all_runs, tc, precision)
1065 1073
1066 1074 if not quiet :
1067 1075 # Check best timing is greater than zero to avoid a
1068 1076 # ZeroDivisionError.
1069 1077 # In cases where the slowest timing is lesser than a micosecond
1070 1078 # we assume that it does not really matter if the fastest
1071 1079 # timing is 4 times faster than the slowest timing or not.
1072 1080 if worst > 4 * best and best > 0 and worst > 1e-6:
1073 1081 print("The slowest run took %0.2f times longer than the "
1074 1082 "fastest. This could mean that an intermediate result "
1075 1083 "is being cached." % (worst / best))
1076 1084
1077 1085 print( timeit_result )
1078 1086
1079 1087 if tc > tc_min:
1080 1088 print("Compiler time: %.2f s" % tc)
1081 1089 if return_result:
1082 1090 return timeit_result
1083 1091
1084 1092 @skip_doctest
1085 1093 @needs_local_scope
1086 1094 @line_cell_magic
1087 1095 def time(self,line='', cell=None, local_ns=None):
1088 1096 """Time execution of a Python statement or expression.
1089 1097
1090 1098 The CPU and wall clock times are printed, and the value of the
1091 1099 expression (if any) is returned. Note that under Win32, system time
1092 1100 is always reported as 0, since it can not be measured.
1093 1101
1094 1102 This function can be used both as a line and cell magic:
1095 1103
1096 1104 - In line mode you can time a single-line statement (though multiple
1097 1105 ones can be chained with using semicolons).
1098 1106
1099 1107 - In cell mode, you can time the cell body (a directly
1100 1108 following statement raises an error).
1101 1109
1102 1110 This function provides very basic timing functionality. Use the timeit
1103 1111 magic for more control over the measurement.
1104 1112
1105 1113 Examples
1106 1114 --------
1107 1115 ::
1108 1116
1109 1117 In [1]: %time 2**128
1110 1118 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1111 1119 Wall time: 0.00
1112 1120 Out[1]: 340282366920938463463374607431768211456L
1113 1121
1114 1122 In [2]: n = 1000000
1115 1123
1116 1124 In [3]: %time sum(range(n))
1117 1125 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1118 1126 Wall time: 1.37
1119 1127 Out[3]: 499999500000L
1120 1128
1121 1129 In [4]: %time print 'hello world'
1122 1130 hello world
1123 1131 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1124 1132 Wall time: 0.00
1125 1133
1126 1134 Note that the time needed by Python to compile the given expression
1127 1135 will be reported if it is more than 0.1s. In this example, the
1128 1136 actual exponentiation is done by Python at compilation time, so while
1129 1137 the expression can take a noticeable amount of time to compute, that
1130 1138 time is purely due to the compilation:
1131 1139
1132 1140 In [5]: %time 3**9999;
1133 1141 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1134 1142 Wall time: 0.00 s
1135 1143
1136 1144 In [6]: %time 3**999999;
1137 1145 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1138 1146 Wall time: 0.00 s
1139 1147 Compiler : 0.78 s
1140 1148 """
1141 1149
1142 1150 # fail immediately if the given expression can't be compiled
1143 1151
1144 1152 if line and cell:
1145 1153 raise UsageError("Can't use statement directly after '%%time'!")
1146 1154
1147 1155 if cell:
1148 1156 expr = self.shell.input_transformer_manager.transform_cell(cell)
1149 1157 else:
1150 1158 expr = self.shell.input_transformer_manager.transform_cell(line)
1151 1159
1152 1160 # Minimum time above which parse time will be reported
1153 1161 tp_min = 0.1
1154 1162
1155 1163 t0 = clock()
1156 1164 expr_ast = self.shell.compile.ast_parse(expr)
1157 1165 tp = clock()-t0
1158 1166
1159 1167 # Apply AST transformations
1160 1168 expr_ast = self.shell.transform_ast(expr_ast)
1161 1169
1162 1170 # Minimum time above which compilation time will be reported
1163 1171 tc_min = 0.1
1164 1172
1165 1173 if len(expr_ast.body)==1 and isinstance(expr_ast.body[0], ast.Expr):
1166 1174 mode = 'eval'
1167 1175 source = '<timed eval>'
1168 1176 expr_ast = ast.Expression(expr_ast.body[0].value)
1169 1177 else:
1170 1178 mode = 'exec'
1171 1179 source = '<timed exec>'
1172 1180 t0 = clock()
1173 1181 code = self.shell.compile(expr_ast, source, mode)
1174 1182 tc = clock()-t0
1175 1183
1176 1184 # skew measurement as little as possible
1177 1185 glob = self.shell.user_ns
1178 1186 wtime = time.time
1179 1187 # time execution
1180 1188 wall_st = wtime()
1181 1189 if mode=='eval':
1182 1190 st = clock2()
1183 1191 try:
1184 1192 out = eval(code, glob, local_ns)
1185 1193 except:
1186 1194 self.shell.showtraceback()
1187 1195 return
1188 1196 end = clock2()
1189 1197 else:
1190 1198 st = clock2()
1191 1199 try:
1192 1200 exec(code, glob, local_ns)
1193 1201 except:
1194 1202 self.shell.showtraceback()
1195 1203 return
1196 1204 end = clock2()
1197 1205 out = None
1198 1206 wall_end = wtime()
1199 1207 # Compute actual times and report
1200 1208 wall_time = wall_end-wall_st
1201 1209 cpu_user = end[0]-st[0]
1202 1210 cpu_sys = end[1]-st[1]
1203 1211 cpu_tot = cpu_user+cpu_sys
1204 1212 # On windows cpu_sys is always zero, so no new information to the next print
1205 1213 if sys.platform != 'win32':
1206 1214 print("CPU times: user %s, sys: %s, total: %s" % \
1207 1215 (_format_time(cpu_user),_format_time(cpu_sys),_format_time(cpu_tot)))
1208 1216 print("Wall time: %s" % _format_time(wall_time))
1209 1217 if tc > tc_min:
1210 1218 print("Compiler : %s" % _format_time(tc))
1211 1219 if tp > tp_min:
1212 1220 print("Parser : %s" % _format_time(tp))
1213 1221 return out
1214 1222
1215 1223 @skip_doctest
1216 1224 @line_magic
1217 1225 def macro(self, parameter_s=''):
1218 1226 """Define a macro for future re-execution. It accepts ranges of history,
1219 1227 filenames or string objects.
1220 1228
1221 1229 Usage:\\
1222 1230 %macro [options] name n1-n2 n3-n4 ... n5 .. n6 ...
1223 1231
1224 1232 Options:
1225 1233
1226 1234 -r: use 'raw' input. By default, the 'processed' history is used,
1227 1235 so that magics are loaded in their transformed version to valid
1228 1236 Python. If this option is given, the raw input as typed at the
1229 1237 command line is used instead.
1230 1238
1231 1239 -q: quiet macro definition. By default, a tag line is printed
1232 1240 to indicate the macro has been created, and then the contents of
1233 1241 the macro are printed. If this option is given, then no printout
1234 1242 is produced once the macro is created.
1235 1243
1236 1244 This will define a global variable called `name` which is a string
1237 1245 made of joining the slices and lines you specify (n1,n2,... numbers
1238 1246 above) from your input history into a single string. This variable
1239 1247 acts like an automatic function which re-executes those lines as if
1240 1248 you had typed them. You just type 'name' at the prompt and the code
1241 1249 executes.
1242 1250
1243 1251 The syntax for indicating input ranges is described in %history.
1244 1252
1245 1253 Note: as a 'hidden' feature, you can also use traditional python slice
1246 1254 notation, where N:M means numbers N through M-1.
1247 1255
1248 1256 For example, if your history contains (print using %hist -n )::
1249 1257
1250 1258 44: x=1
1251 1259 45: y=3
1252 1260 46: z=x+y
1253 1261 47: print x
1254 1262 48: a=5
1255 1263 49: print 'x',x,'y',y
1256 1264
1257 1265 you can create a macro with lines 44 through 47 (included) and line 49
1258 1266 called my_macro with::
1259 1267
1260 1268 In [55]: %macro my_macro 44-47 49
1261 1269
1262 1270 Now, typing `my_macro` (without quotes) will re-execute all this code
1263 1271 in one pass.
1264 1272
1265 1273 You don't need to give the line-numbers in order, and any given line
1266 1274 number can appear multiple times. You can assemble macros with any
1267 1275 lines from your input history in any order.
1268 1276
1269 1277 The macro is a simple object which holds its value in an attribute,
1270 1278 but IPython's display system checks for macros and executes them as
1271 1279 code instead of printing them when you type their name.
1272 1280
1273 1281 You can view a macro's contents by explicitly printing it with::
1274 1282
1275 1283 print macro_name
1276 1284
1277 1285 """
1278 1286 opts,args = self.parse_options(parameter_s,'rq',mode='list')
1279 1287 if not args: # List existing macros
1280 1288 return sorted(k for k,v in self.shell.user_ns.items() if isinstance(v, Macro))
1281 1289 if len(args) == 1:
1282 1290 raise UsageError(
1283 1291 "%macro insufficient args; usage '%macro name n1-n2 n3-4...")
1284 1292 name, codefrom = args[0], " ".join(args[1:])
1285 1293
1286 1294 #print 'rng',ranges # dbg
1287 1295 try:
1288 1296 lines = self.shell.find_user_code(codefrom, 'r' in opts)
1289 1297 except (ValueError, TypeError) as e:
1290 1298 print(e.args[0])
1291 1299 return
1292 1300 macro = Macro(lines)
1293 1301 self.shell.define_macro(name, macro)
1294 1302 if not ( 'q' in opts) :
1295 1303 print('Macro `%s` created. To execute, type its name (without quotes).' % name)
1296 1304 print('=== Macro contents: ===')
1297 1305 print(macro, end=' ')
1298 1306
1299 1307 @magic_arguments.magic_arguments()
1300 1308 @magic_arguments.argument('output', type=str, default='', nargs='?',
1301 1309 help="""The name of the variable in which to store output.
1302 1310 This is a utils.io.CapturedIO object with stdout/err attributes
1303 1311 for the text of the captured output.
1304 1312
1305 1313 CapturedOutput also has a show() method for displaying the output,
1306 1314 and __call__ as well, so you can use that to quickly display the
1307 1315 output.
1308 1316
1309 1317 If unspecified, captured output is discarded.
1310 1318 """
1311 1319 )
1312 1320 @magic_arguments.argument('--no-stderr', action="store_true",
1313 1321 help="""Don't capture stderr."""
1314 1322 )
1315 1323 @magic_arguments.argument('--no-stdout', action="store_true",
1316 1324 help="""Don't capture stdout."""
1317 1325 )
1318 1326 @magic_arguments.argument('--no-display', action="store_true",
1319 1327 help="""Don't capture IPython's rich display."""
1320 1328 )
1321 1329 @cell_magic
1322 1330 def capture(self, line, cell):
1323 1331 """run the cell, capturing stdout, stderr, and IPython's rich display() calls."""
1324 1332 args = magic_arguments.parse_argstring(self.capture, line)
1325 1333 out = not args.no_stdout
1326 1334 err = not args.no_stderr
1327 1335 disp = not args.no_display
1328 1336 with capture_output(out, err, disp) as io:
1329 1337 self.shell.run_cell(cell)
1330 1338 if args.output:
1331 1339 self.shell.user_ns[args.output] = io
1332 1340
1333 1341 def parse_breakpoint(text, current_file):
1334 1342 '''Returns (file, line) for file:line and (current_file, line) for line'''
1335 1343 colon = text.find(':')
1336 1344 if colon == -1:
1337 1345 return current_file, int(text)
1338 1346 else:
1339 1347 return text[:colon], int(text[colon+1:])
1340 1348
1341 1349 def _format_time(timespan, precision=3):
1342 1350 """Formats the timespan in a human readable form"""
1343 1351
1344 1352 if timespan >= 60.0:
1345 1353 # we have more than a minute, format that in a human readable form
1346 1354 # Idea from http://snipplr.com/view/5713/
1347 1355 parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
1348 1356 time = []
1349 1357 leftover = timespan
1350 1358 for suffix, length in parts:
1351 1359 value = int(leftover / length)
1352 1360 if value > 0:
1353 1361 leftover = leftover % length
1354 1362 time.append(u'%s%s' % (str(value), suffix))
1355 1363 if leftover < 1:
1356 1364 break
1357 1365 return " ".join(time)
1358 1366
1359 1367
1360 1368 # Unfortunately the unicode 'micro' symbol can cause problems in
1361 1369 # certain terminals.
1362 1370 # See bug: https://bugs.launchpad.net/ipython/+bug/348466
1363 1371 # Try to prevent crashes by being more secure than it needs to
1364 1372 # E.g. eclipse is able to print a µ, but has no sys.stdout.encoding set.
1365 1373 units = [u"s", u"ms",u'us',"ns"] # the save value
1366 1374 if hasattr(sys.stdout, 'encoding') and sys.stdout.encoding:
1367 1375 try:
1368 1376 u'\xb5'.encode(sys.stdout.encoding)
1369 1377 units = [u"s", u"ms",u'\xb5s',"ns"]
1370 1378 except:
1371 1379 pass
1372 1380 scaling = [1, 1e3, 1e6, 1e9]
1373 1381
1374 1382 if timespan > 0.0:
1375 1383 order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
1376 1384 else:
1377 1385 order = 3
1378 1386 return u"%.*g %s" % (precision, timespan * scaling[order], units[order])
@@ -1,899 +1,899 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Tests for the key interactiveshell module.
3 3
4 4 Historically the main classes in interactiveshell have been under-tested. This
5 5 module should grow as many single-method tests as possible to trap many of the
6 6 recurring bugs we seem to encounter with high-level interaction.
7 7 """
8 8
9 9 # Copyright (c) IPython Development Team.
10 10 # Distributed under the terms of the Modified BSD License.
11 11
12 12 import ast
13 13 import os
14 14 import signal
15 15 import shutil
16 16 import sys
17 17 import tempfile
18 18 import unittest
19 19 from unittest import mock
20 20 from io import StringIO
21 21
22 22 from os.path import join
23 23
24 24 import nose.tools as nt
25 25
26 26 from IPython.core.error import InputRejected
27 27 from IPython.core.inputtransformer import InputTransformer
28 28 from IPython.testing.decorators import (
29 29 skipif, skip_win32, onlyif_unicode_paths, onlyif_cmds_exist,
30 30 )
31 31 from IPython.testing import tools as tt
32 32 from IPython.utils.process import find_cmd
33 33 from IPython.utils import py3compat
34 34
35 35 #-----------------------------------------------------------------------------
36 36 # Globals
37 37 #-----------------------------------------------------------------------------
38 38 # This is used by every single test, no point repeating it ad nauseam
39 39 ip = get_ipython()
40 40
41 41 #-----------------------------------------------------------------------------
42 42 # Tests
43 43 #-----------------------------------------------------------------------------
44 44
45 45 class DerivedInterrupt(KeyboardInterrupt):
46 46 pass
47 47
48 48 class InteractiveShellTestCase(unittest.TestCase):
49 49 def test_naked_string_cells(self):
50 50 """Test that cells with only naked strings are fully executed"""
51 51 # First, single-line inputs
52 52 ip.run_cell('"a"\n')
53 53 self.assertEqual(ip.user_ns['_'], 'a')
54 54 # And also multi-line cells
55 55 ip.run_cell('"""a\nb"""\n')
56 56 self.assertEqual(ip.user_ns['_'], 'a\nb')
57 57
58 58 def test_run_empty_cell(self):
59 59 """Just make sure we don't get a horrible error with a blank
60 60 cell of input. Yes, I did overlook that."""
61 61 old_xc = ip.execution_count
62 62 res = ip.run_cell('')
63 63 self.assertEqual(ip.execution_count, old_xc)
64 64 self.assertEqual(res.execution_count, None)
65 65
66 66 def test_run_cell_multiline(self):
67 67 """Multi-block, multi-line cells must execute correctly.
68 68 """
69 69 src = '\n'.join(["x=1",
70 70 "y=2",
71 71 "if 1:",
72 72 " x += 1",
73 73 " y += 1",])
74 74 res = ip.run_cell(src)
75 75 self.assertEqual(ip.user_ns['x'], 2)
76 76 self.assertEqual(ip.user_ns['y'], 3)
77 77 self.assertEqual(res.success, True)
78 78 self.assertEqual(res.result, None)
79 79
80 80 def test_multiline_string_cells(self):
81 81 "Code sprinkled with multiline strings should execute (GH-306)"
82 82 ip.run_cell('tmp=0')
83 83 self.assertEqual(ip.user_ns['tmp'], 0)
84 84 res = ip.run_cell('tmp=1;"""a\nb"""\n')
85 85 self.assertEqual(ip.user_ns['tmp'], 1)
86 86 self.assertEqual(res.success, True)
87 87 self.assertEqual(res.result, "a\nb")
88 88
89 89 def test_dont_cache_with_semicolon(self):
90 90 "Ending a line with semicolon should not cache the returned object (GH-307)"
91 91 oldlen = len(ip.user_ns['Out'])
92 92 for cell in ['1;', '1;1;']:
93 93 res = ip.run_cell(cell, store_history=True)
94 94 newlen = len(ip.user_ns['Out'])
95 95 self.assertEqual(oldlen, newlen)
96 96 self.assertIsNone(res.result)
97 97 i = 0
98 98 #also test the default caching behavior
99 99 for cell in ['1', '1;1']:
100 100 ip.run_cell(cell, store_history=True)
101 101 newlen = len(ip.user_ns['Out'])
102 102 i += 1
103 103 self.assertEqual(oldlen+i, newlen)
104 104
105 105 def test_syntax_error(self):
106 106 res = ip.run_cell("raise = 3")
107 107 self.assertIsInstance(res.error_before_exec, SyntaxError)
108 108
109 109 def test_In_variable(self):
110 110 "Verify that In variable grows with user input (GH-284)"
111 111 oldlen = len(ip.user_ns['In'])
112 112 ip.run_cell('1;', store_history=True)
113 113 newlen = len(ip.user_ns['In'])
114 114 self.assertEqual(oldlen+1, newlen)
115 115 self.assertEqual(ip.user_ns['In'][-1],'1;')
116 116
117 117 def test_magic_names_in_string(self):
118 118 ip.run_cell('a = """\n%exit\n"""')
119 119 self.assertEqual(ip.user_ns['a'], '\n%exit\n')
120 120
121 121 def test_trailing_newline(self):
122 122 """test that running !(command) does not raise a SyntaxError"""
123 123 ip.run_cell('!(true)\n', False)
124 124 ip.run_cell('!(true)\n\n\n', False)
125 125
126 126 def test_gh_597(self):
127 127 """Pretty-printing lists of objects with non-ascii reprs may cause
128 128 problems."""
129 129 class Spam(object):
130 130 def __repr__(self):
131 131 return "\xe9"*50
132 132 import IPython.core.formatters
133 133 f = IPython.core.formatters.PlainTextFormatter()
134 134 f([Spam(),Spam()])
135 135
136 136
137 137 def test_future_flags(self):
138 138 """Check that future flags are used for parsing code (gh-777)"""
139 139 ip.run_cell('from __future__ import barry_as_FLUFL')
140 140 try:
141 141 ip.run_cell('prfunc_return_val = 1 <> 2')
142 142 assert 'prfunc_return_val' in ip.user_ns
143 143 finally:
144 144 # Reset compiler flags so we don't mess up other tests.
145 145 ip.compile.reset_compiler_flags()
146 146
147 147 def test_can_pickle(self):
148 148 "Can we pickle objects defined interactively (GH-29)"
149 149 ip = get_ipython()
150 150 ip.reset()
151 151 ip.run_cell(("class Mylist(list):\n"
152 152 " def __init__(self,x=[]):\n"
153 153 " list.__init__(self,x)"))
154 154 ip.run_cell("w=Mylist([1,2,3])")
155 155
156 156 from pickle import dumps
157 157
158 158 # We need to swap in our main module - this is only necessary
159 159 # inside the test framework, because IPython puts the interactive module
160 160 # in place (but the test framework undoes this).
161 161 _main = sys.modules['__main__']
162 162 sys.modules['__main__'] = ip.user_module
163 163 try:
164 164 res = dumps(ip.user_ns["w"])
165 165 finally:
166 166 sys.modules['__main__'] = _main
167 167 self.assertTrue(isinstance(res, bytes))
168 168
169 169 def test_global_ns(self):
170 170 "Code in functions must be able to access variables outside them."
171 171 ip = get_ipython()
172 172 ip.run_cell("a = 10")
173 173 ip.run_cell(("def f(x):\n"
174 174 " return x + a"))
175 175 ip.run_cell("b = f(12)")
176 176 self.assertEqual(ip.user_ns["b"], 22)
177 177
178 178 def test_bad_custom_tb(self):
179 179 """Check that InteractiveShell is protected from bad custom exception handlers"""
180 180 ip.set_custom_exc((IOError,), lambda etype,value,tb: 1/0)
181 181 self.assertEqual(ip.custom_exceptions, (IOError,))
182 182 with tt.AssertPrints("Custom TB Handler failed", channel='stderr'):
183 183 ip.run_cell(u'raise IOError("foo")')
184 184 self.assertEqual(ip.custom_exceptions, ())
185 185
186 186 def test_bad_custom_tb_return(self):
187 187 """Check that InteractiveShell is protected from bad return types in custom exception handlers"""
188 188 ip.set_custom_exc((NameError,),lambda etype,value,tb, tb_offset=None: 1)
189 189 self.assertEqual(ip.custom_exceptions, (NameError,))
190 190 with tt.AssertPrints("Custom TB Handler failed", channel='stderr'):
191 191 ip.run_cell(u'a=abracadabra')
192 192 self.assertEqual(ip.custom_exceptions, ())
193 193
194 194 def test_drop_by_id(self):
195 195 myvars = {"a":object(), "b":object(), "c": object()}
196 196 ip.push(myvars, interactive=False)
197 197 for name in myvars:
198 198 assert name in ip.user_ns, name
199 199 assert name in ip.user_ns_hidden, name
200 200 ip.user_ns['b'] = 12
201 201 ip.drop_by_id(myvars)
202 202 for name in ["a", "c"]:
203 203 assert name not in ip.user_ns, name
204 204 assert name not in ip.user_ns_hidden, name
205 205 assert ip.user_ns['b'] == 12
206 206 ip.reset()
207 207
208 208 def test_var_expand(self):
209 209 ip.user_ns['f'] = u'Ca\xf1o'
210 210 self.assertEqual(ip.var_expand(u'echo $f'), u'echo Ca\xf1o')
211 211 self.assertEqual(ip.var_expand(u'echo {f}'), u'echo Ca\xf1o')
212 212 self.assertEqual(ip.var_expand(u'echo {f[:-1]}'), u'echo Ca\xf1')
213 213 self.assertEqual(ip.var_expand(u'echo {1*2}'), u'echo 2')
214 214
215 215 ip.user_ns['f'] = b'Ca\xc3\xb1o'
216 216 # This should not raise any exception:
217 217 ip.var_expand(u'echo $f')
218 218
219 219 def test_var_expand_local(self):
220 220 """Test local variable expansion in !system and %magic calls"""
221 221 # !system
222 222 ip.run_cell('def test():\n'
223 223 ' lvar = "ttt"\n'
224 224 ' ret = !echo {lvar}\n'
225 225 ' return ret[0]\n')
226 226 res = ip.user_ns['test']()
227 227 nt.assert_in('ttt', res)
228 228
229 229 # %magic
230 230 ip.run_cell('def makemacro():\n'
231 231 ' macroname = "macro_var_expand_locals"\n'
232 232 ' %macro {macroname} codestr\n')
233 233 ip.user_ns['codestr'] = "str(12)"
234 234 ip.run_cell('makemacro()')
235 235 nt.assert_in('macro_var_expand_locals', ip.user_ns)
236 236
237 237 def test_var_expand_self(self):
238 238 """Test variable expansion with the name 'self', which was failing.
239 239
240 240 See https://github.com/ipython/ipython/issues/1878#issuecomment-7698218
241 241 """
242 242 ip.run_cell('class cTest:\n'
243 243 ' classvar="see me"\n'
244 244 ' def test(self):\n'
245 245 ' res = !echo Variable: {self.classvar}\n'
246 246 ' return res[0]\n')
247 247 nt.assert_in('see me', ip.user_ns['cTest']().test())
248 248
249 249 def test_bad_var_expand(self):
250 250 """var_expand on invalid formats shouldn't raise"""
251 251 # SyntaxError
252 252 self.assertEqual(ip.var_expand(u"{'a':5}"), u"{'a':5}")
253 253 # NameError
254 254 self.assertEqual(ip.var_expand(u"{asdf}"), u"{asdf}")
255 255 # ZeroDivisionError
256 256 self.assertEqual(ip.var_expand(u"{1/0}"), u"{1/0}")
257 257
258 258 def test_silent_postexec(self):
259 259 """run_cell(silent=True) doesn't invoke pre/post_run_cell callbacks"""
260 260 pre_explicit = mock.Mock()
261 261 pre_always = mock.Mock()
262 262 post_explicit = mock.Mock()
263 263 post_always = mock.Mock()
264 264
265 265 ip.events.register('pre_run_cell', pre_explicit)
266 266 ip.events.register('pre_execute', pre_always)
267 267 ip.events.register('post_run_cell', post_explicit)
268 268 ip.events.register('post_execute', post_always)
269 269
270 270 try:
271 271 ip.run_cell("1", silent=True)
272 272 assert pre_always.called
273 273 assert not pre_explicit.called
274 274 assert post_always.called
275 275 assert not post_explicit.called
276 276 # double-check that non-silent exec did what we expected
277 277 # silent to avoid
278 278 ip.run_cell("1")
279 279 assert pre_explicit.called
280 280 assert post_explicit.called
281 281 finally:
282 282 # remove post-exec
283 283 ip.events.unregister('pre_run_cell', pre_explicit)
284 284 ip.events.unregister('pre_execute', pre_always)
285 285 ip.events.unregister('post_run_cell', post_explicit)
286 286 ip.events.unregister('post_execute', post_always)
287 287
288 288 def test_silent_noadvance(self):
289 289 """run_cell(silent=True) doesn't advance execution_count"""
290 290 ec = ip.execution_count
291 291 # silent should force store_history=False
292 292 ip.run_cell("1", store_history=True, silent=True)
293 293
294 294 self.assertEqual(ec, ip.execution_count)
295 295 # double-check that non-silent exec did what we expected
296 296 # silent to avoid
297 297 ip.run_cell("1", store_history=True)
298 298 self.assertEqual(ec+1, ip.execution_count)
299 299
300 300 def test_silent_nodisplayhook(self):
301 301 """run_cell(silent=True) doesn't trigger displayhook"""
302 302 d = dict(called=False)
303 303
304 304 trap = ip.display_trap
305 305 save_hook = trap.hook
306 306
307 307 def failing_hook(*args, **kwargs):
308 308 d['called'] = True
309 309
310 310 try:
311 311 trap.hook = failing_hook
312 312 res = ip.run_cell("1", silent=True)
313 313 self.assertFalse(d['called'])
314 314 self.assertIsNone(res.result)
315 315 # double-check that non-silent exec did what we expected
316 316 # silent to avoid
317 317 ip.run_cell("1")
318 318 self.assertTrue(d['called'])
319 319 finally:
320 320 trap.hook = save_hook
321 321
322 322 def test_ofind_line_magic(self):
323 323 from IPython.core.magic import register_line_magic
324 324
325 325 @register_line_magic
326 326 def lmagic(line):
327 327 "A line magic"
328 328
329 329 # Get info on line magic
330 330 lfind = ip._ofind('lmagic')
331 331 info = dict(found=True, isalias=False, ismagic=True,
332 332 namespace = 'IPython internal', obj= lmagic.__wrapped__,
333 333 parent = None)
334 334 nt.assert_equal(lfind, info)
335 335
336 336 def test_ofind_cell_magic(self):
337 337 from IPython.core.magic import register_cell_magic
338 338
339 339 @register_cell_magic
340 340 def cmagic(line, cell):
341 341 "A cell magic"
342 342
343 343 # Get info on cell magic
344 344 find = ip._ofind('cmagic')
345 345 info = dict(found=True, isalias=False, ismagic=True,
346 346 namespace = 'IPython internal', obj= cmagic.__wrapped__,
347 347 parent = None)
348 348 nt.assert_equal(find, info)
349 349
350 350 def test_ofind_property_with_error(self):
351 351 class A(object):
352 352 @property
353 353 def foo(self):
354 354 raise NotImplementedError()
355 355 a = A()
356 356
357 357 found = ip._ofind('a.foo', [('locals', locals())])
358 358 info = dict(found=True, isalias=False, ismagic=False,
359 359 namespace='locals', obj=A.foo, parent=a)
360 360 nt.assert_equal(found, info)
361 361
362 362 def test_ofind_multiple_attribute_lookups(self):
363 363 class A(object):
364 364 @property
365 365 def foo(self):
366 366 raise NotImplementedError()
367 367
368 368 a = A()
369 369 a.a = A()
370 370 a.a.a = A()
371 371
372 372 found = ip._ofind('a.a.a.foo', [('locals', locals())])
373 373 info = dict(found=True, isalias=False, ismagic=False,
374 374 namespace='locals', obj=A.foo, parent=a.a.a)
375 375 nt.assert_equal(found, info)
376 376
377 377 def test_ofind_slotted_attributes(self):
378 378 class A(object):
379 379 __slots__ = ['foo']
380 380 def __init__(self):
381 381 self.foo = 'bar'
382 382
383 383 a = A()
384 384 found = ip._ofind('a.foo', [('locals', locals())])
385 385 info = dict(found=True, isalias=False, ismagic=False,
386 386 namespace='locals', obj=a.foo, parent=a)
387 387 nt.assert_equal(found, info)
388 388
389 389 found = ip._ofind('a.bar', [('locals', locals())])
390 390 info = dict(found=False, isalias=False, ismagic=False,
391 391 namespace=None, obj=None, parent=a)
392 392 nt.assert_equal(found, info)
393 393
394 394 def test_ofind_prefers_property_to_instance_level_attribute(self):
395 395 class A(object):
396 396 @property
397 397 def foo(self):
398 398 return 'bar'
399 399 a = A()
400 400 a.__dict__['foo'] = 'baz'
401 401 nt.assert_equal(a.foo, 'bar')
402 402 found = ip._ofind('a.foo', [('locals', locals())])
403 403 nt.assert_is(found['obj'], A.foo)
404 404
405 405 def test_custom_syntaxerror_exception(self):
406 406 called = []
407 407 def my_handler(shell, etype, value, tb, tb_offset=None):
408 408 called.append(etype)
409 409 shell.showtraceback((etype, value, tb), tb_offset=tb_offset)
410 410
411 411 ip.set_custom_exc((SyntaxError,), my_handler)
412 412 try:
413 413 ip.run_cell("1f")
414 414 # Check that this was called, and only once.
415 415 self.assertEqual(called, [SyntaxError])
416 416 finally:
417 417 # Reset the custom exception hook
418 418 ip.set_custom_exc((), None)
419 419
420 420 def test_custom_exception(self):
421 421 called = []
422 422 def my_handler(shell, etype, value, tb, tb_offset=None):
423 423 called.append(etype)
424 424 shell.showtraceback((etype, value, tb), tb_offset=tb_offset)
425 425
426 426 ip.set_custom_exc((ValueError,), my_handler)
427 427 try:
428 428 res = ip.run_cell("raise ValueError('test')")
429 429 # Check that this was called, and only once.
430 430 self.assertEqual(called, [ValueError])
431 431 # Check that the error is on the result object
432 432 self.assertIsInstance(res.error_in_exec, ValueError)
433 433 finally:
434 434 # Reset the custom exception hook
435 435 ip.set_custom_exc((), None)
436 436
437 437 def test_mktempfile(self):
438 438 filename = ip.mktempfile()
439 439 # Check that we can open the file again on Windows
440 440 with open(filename, 'w') as f:
441 441 f.write('abc')
442 442
443 443 filename = ip.mktempfile(data='blah')
444 444 with open(filename, 'r') as f:
445 445 self.assertEqual(f.read(), 'blah')
446 446
447 447 def test_new_main_mod(self):
448 448 # Smoketest to check that this accepts a unicode module name
449 449 name = u'jiefmw'
450 450 mod = ip.new_main_mod(u'%s.py' % name, name)
451 451 self.assertEqual(mod.__name__, name)
452 452
453 453 def test_get_exception_only(self):
454 454 try:
455 455 raise KeyboardInterrupt
456 456 except KeyboardInterrupt:
457 457 msg = ip.get_exception_only()
458 458 self.assertEqual(msg, 'KeyboardInterrupt\n')
459 459
460 460 try:
461 461 raise DerivedInterrupt("foo")
462 462 except KeyboardInterrupt:
463 463 msg = ip.get_exception_only()
464 464 self.assertEqual(msg, 'IPython.core.tests.test_interactiveshell.DerivedInterrupt: foo\n')
465 465
466 466 def test_inspect_text(self):
467 467 ip.run_cell('a = 5')
468 468 text = ip.object_inspect_text('a')
469 469 self.assertIsInstance(text, str)
470 470
471 471
472 472 class TestSafeExecfileNonAsciiPath(unittest.TestCase):
473 473
474 474 @onlyif_unicode_paths
475 475 def setUp(self):
476 476 self.BASETESTDIR = tempfile.mkdtemp()
477 477 self.TESTDIR = join(self.BASETESTDIR, u"åäö")
478 478 os.mkdir(self.TESTDIR)
479 479 with open(join(self.TESTDIR, u"åäötestscript.py"), "w") as sfile:
480 480 sfile.write("pass\n")
481 481 self.oldpath = os.getcwd()
482 482 os.chdir(self.TESTDIR)
483 483 self.fname = u"åäötestscript.py"
484 484
485 485 def tearDown(self):
486 486 os.chdir(self.oldpath)
487 487 shutil.rmtree(self.BASETESTDIR)
488 488
489 489 @onlyif_unicode_paths
490 490 def test_1(self):
491 491 """Test safe_execfile with non-ascii path
492 492 """
493 493 ip.safe_execfile(self.fname, {}, raise_exceptions=True)
494 494
495 495 class ExitCodeChecks(tt.TempFileMixin):
496 496 def test_exit_code_ok(self):
497 497 self.system('exit 0')
498 498 self.assertEqual(ip.user_ns['_exit_code'], 0)
499 499
500 500 def test_exit_code_error(self):
501 501 self.system('exit 1')
502 502 self.assertEqual(ip.user_ns['_exit_code'], 1)
503 503
504 504 @skipif(not hasattr(signal, 'SIGALRM'))
505 505 def test_exit_code_signal(self):
506 506 self.mktmp("import signal, time\n"
507 507 "signal.setitimer(signal.ITIMER_REAL, 0.1)\n"
508 508 "time.sleep(1)\n")
509 509 self.system("%s %s" % (sys.executable, self.fname))
510 510 self.assertEqual(ip.user_ns['_exit_code'], -signal.SIGALRM)
511 511
512 512 @onlyif_cmds_exist("csh")
513 513 def test_exit_code_signal_csh(self):
514 514 SHELL = os.environ.get('SHELL', None)
515 515 os.environ['SHELL'] = find_cmd("csh")
516 516 try:
517 517 self.test_exit_code_signal()
518 518 finally:
519 519 if SHELL is not None:
520 520 os.environ['SHELL'] = SHELL
521 521 else:
522 522 del os.environ['SHELL']
523 523
524 524 class TestSystemRaw(unittest.TestCase, ExitCodeChecks):
525 525 system = ip.system_raw
526 526
527 527 @onlyif_unicode_paths
528 528 def test_1(self):
529 529 """Test system_raw with non-ascii cmd
530 530 """
531 531 cmd = u'''python -c "'åäö'" '''
532 532 ip.system_raw(cmd)
533 533
534 534 @mock.patch('subprocess.call', side_effect=KeyboardInterrupt)
535 535 @mock.patch('os.system', side_effect=KeyboardInterrupt)
536 536 def test_control_c(self, *mocks):
537 537 try:
538 538 self.system("sleep 1 # wont happen")
539 539 except KeyboardInterrupt:
540 540 self.fail("system call should intercept "
541 541 "keyboard interrupt from subprocess.call")
542 542 self.assertEqual(ip.user_ns['_exit_code'], -signal.SIGINT)
543 543
544 544 # TODO: Exit codes are currently ignored on Windows.
545 545 class TestSystemPipedExitCode(unittest.TestCase, ExitCodeChecks):
546 546 system = ip.system_piped
547 547
548 548 @skip_win32
549 549 def test_exit_code_ok(self):
550 550 ExitCodeChecks.test_exit_code_ok(self)
551 551
552 552 @skip_win32
553 553 def test_exit_code_error(self):
554 554 ExitCodeChecks.test_exit_code_error(self)
555 555
556 556 @skip_win32
557 557 def test_exit_code_signal(self):
558 558 ExitCodeChecks.test_exit_code_signal(self)
559 559
560 560 class TestModules(unittest.TestCase, tt.TempFileMixin):
561 561 def test_extraneous_loads(self):
562 562 """Test we're not loading modules on startup that we shouldn't.
563 563 """
564 564 self.mktmp("import sys\n"
565 565 "print('numpy' in sys.modules)\n"
566 566 "print('ipyparallel' in sys.modules)\n"
567 567 "print('ipykernel' in sys.modules)\n"
568 568 )
569 569 out = "False\nFalse\nFalse\n"
570 570 tt.ipexec_validate(self.fname, out)
571 571
572 572 class Negator(ast.NodeTransformer):
573 573 """Negates all number literals in an AST."""
574 574 def visit_Num(self, node):
575 575 node.n = -node.n
576 576 return node
577 577
578 578 class TestAstTransform(unittest.TestCase):
579 579 def setUp(self):
580 580 self.negator = Negator()
581 581 ip.ast_transformers.append(self.negator)
582 582
583 583 def tearDown(self):
584 584 ip.ast_transformers.remove(self.negator)
585 585
586 586 def test_run_cell(self):
587 587 with tt.AssertPrints('-34'):
588 588 ip.run_cell('print (12 + 22)')
589 589
590 590 # A named reference to a number shouldn't be transformed.
591 591 ip.user_ns['n'] = 55
592 592 with tt.AssertNotPrints('-55'):
593 593 ip.run_cell('print (n)')
594 594
595 595 def test_timeit(self):
596 596 called = set()
597 597 def f(x):
598 598 called.add(x)
599 599 ip.push({'f':f})
600 600
601 with tt.AssertPrints("mean ± std"):
601 with tt.AssertPrints("std. dev. of"):
602 602 ip.run_line_magic("timeit", "-n1 f(1)")
603 603 self.assertEqual(called, {-1})
604 604 called.clear()
605 605
606 with tt.AssertPrints("mean ± std"):
606 with tt.AssertPrints("std. dev. of"):
607 607 ip.run_cell_magic("timeit", "-n1 f(2)", "f(3)")
608 608 self.assertEqual(called, {-2, -3})
609 609
610 610 def test_time(self):
611 611 called = []
612 612 def f(x):
613 613 called.append(x)
614 614 ip.push({'f':f})
615 615
616 616 # Test with an expression
617 617 with tt.AssertPrints("Wall time: "):
618 618 ip.run_line_magic("time", "f(5+9)")
619 619 self.assertEqual(called, [-14])
620 620 called[:] = []
621 621
622 622 # Test with a statement (different code path)
623 623 with tt.AssertPrints("Wall time: "):
624 624 ip.run_line_magic("time", "a = f(-3 + -2)")
625 625 self.assertEqual(called, [5])
626 626
627 627 def test_macro(self):
628 628 ip.push({'a':10})
629 629 # The AST transformation makes this do a+=-1
630 630 ip.define_macro("amacro", "a+=1\nprint(a)")
631 631
632 632 with tt.AssertPrints("9"):
633 633 ip.run_cell("amacro")
634 634 with tt.AssertPrints("8"):
635 635 ip.run_cell("amacro")
636 636
637 637 class IntegerWrapper(ast.NodeTransformer):
638 638 """Wraps all integers in a call to Integer()"""
639 639 def visit_Num(self, node):
640 640 if isinstance(node.n, int):
641 641 return ast.Call(func=ast.Name(id='Integer', ctx=ast.Load()),
642 642 args=[node], keywords=[])
643 643 return node
644 644
645 645 class TestAstTransform2(unittest.TestCase):
646 646 def setUp(self):
647 647 self.intwrapper = IntegerWrapper()
648 648 ip.ast_transformers.append(self.intwrapper)
649 649
650 650 self.calls = []
651 651 def Integer(*args):
652 652 self.calls.append(args)
653 653 return args
654 654 ip.push({"Integer": Integer})
655 655
656 656 def tearDown(self):
657 657 ip.ast_transformers.remove(self.intwrapper)
658 658 del ip.user_ns['Integer']
659 659
660 660 def test_run_cell(self):
661 661 ip.run_cell("n = 2")
662 662 self.assertEqual(self.calls, [(2,)])
663 663
664 664 # This shouldn't throw an error
665 665 ip.run_cell("o = 2.0")
666 666 self.assertEqual(ip.user_ns['o'], 2.0)
667 667
668 668 def test_timeit(self):
669 669 called = set()
670 670 def f(x):
671 671 called.add(x)
672 672 ip.push({'f':f})
673 673
674 with tt.AssertPrints("mean ± std"):
674 with tt.AssertPrints("std. dev. of"):
675 675 ip.run_line_magic("timeit", "-n1 f(1)")
676 676 self.assertEqual(called, {(1,)})
677 677 called.clear()
678 678
679 with tt.AssertPrints("mean ± std"):
679 with tt.AssertPrints("std. dev. of"):
680 680 ip.run_cell_magic("timeit", "-n1 f(2)", "f(3)")
681 681 self.assertEqual(called, {(2,), (3,)})
682 682
683 683 class ErrorTransformer(ast.NodeTransformer):
684 684 """Throws an error when it sees a number."""
685 685 def visit_Num(self, node):
686 686 raise ValueError("test")
687 687
688 688 class TestAstTransformError(unittest.TestCase):
689 689 def test_unregistering(self):
690 690 err_transformer = ErrorTransformer()
691 691 ip.ast_transformers.append(err_transformer)
692 692
693 693 with tt.AssertPrints("unregister", channel='stderr'):
694 694 ip.run_cell("1 + 2")
695 695
696 696 # This should have been removed.
697 697 nt.assert_not_in(err_transformer, ip.ast_transformers)
698 698
699 699
700 700 class StringRejector(ast.NodeTransformer):
701 701 """Throws an InputRejected when it sees a string literal.
702 702
703 703 Used to verify that NodeTransformers can signal that a piece of code should
704 704 not be executed by throwing an InputRejected.
705 705 """
706 706
707 707 def visit_Str(self, node):
708 708 raise InputRejected("test")
709 709
710 710
711 711 class TestAstTransformInputRejection(unittest.TestCase):
712 712
713 713 def setUp(self):
714 714 self.transformer = StringRejector()
715 715 ip.ast_transformers.append(self.transformer)
716 716
717 717 def tearDown(self):
718 718 ip.ast_transformers.remove(self.transformer)
719 719
720 720 def test_input_rejection(self):
721 721 """Check that NodeTransformers can reject input."""
722 722
723 723 expect_exception_tb = tt.AssertPrints("InputRejected: test")
724 724 expect_no_cell_output = tt.AssertNotPrints("'unsafe'", suppress=False)
725 725
726 726 # Run the same check twice to verify that the transformer is not
727 727 # disabled after raising.
728 728 with expect_exception_tb, expect_no_cell_output:
729 729 ip.run_cell("'unsafe'")
730 730
731 731 with expect_exception_tb, expect_no_cell_output:
732 732 res = ip.run_cell("'unsafe'")
733 733
734 734 self.assertIsInstance(res.error_before_exec, InputRejected)
735 735
736 736 def test__IPYTHON__():
737 737 # This shouldn't raise a NameError, that's all
738 738 __IPYTHON__
739 739
740 740
741 741 class DummyRepr(object):
742 742 def __repr__(self):
743 743 return "DummyRepr"
744 744
745 745 def _repr_html_(self):
746 746 return "<b>dummy</b>"
747 747
748 748 def _repr_javascript_(self):
749 749 return "console.log('hi');", {'key': 'value'}
750 750
751 751
752 752 def test_user_variables():
753 753 # enable all formatters
754 754 ip.display_formatter.active_types = ip.display_formatter.format_types
755 755
756 756 ip.user_ns['dummy'] = d = DummyRepr()
757 757 keys = {'dummy', 'doesnotexist'}
758 758 r = ip.user_expressions({ key:key for key in keys})
759 759
760 760 nt.assert_equal(keys, set(r.keys()))
761 761 dummy = r['dummy']
762 762 nt.assert_equal({'status', 'data', 'metadata'}, set(dummy.keys()))
763 763 nt.assert_equal(dummy['status'], 'ok')
764 764 data = dummy['data']
765 765 metadata = dummy['metadata']
766 766 nt.assert_equal(data.get('text/html'), d._repr_html_())
767 767 js, jsmd = d._repr_javascript_()
768 768 nt.assert_equal(data.get('application/javascript'), js)
769 769 nt.assert_equal(metadata.get('application/javascript'), jsmd)
770 770
771 771 dne = r['doesnotexist']
772 772 nt.assert_equal(dne['status'], 'error')
773 773 nt.assert_equal(dne['ename'], 'NameError')
774 774
775 775 # back to text only
776 776 ip.display_formatter.active_types = ['text/plain']
777 777
778 778 def test_user_expression():
779 779 # enable all formatters
780 780 ip.display_formatter.active_types = ip.display_formatter.format_types
781 781 query = {
782 782 'a' : '1 + 2',
783 783 'b' : '1/0',
784 784 }
785 785 r = ip.user_expressions(query)
786 786 import pprint
787 787 pprint.pprint(r)
788 788 nt.assert_equal(set(r.keys()), set(query.keys()))
789 789 a = r['a']
790 790 nt.assert_equal({'status', 'data', 'metadata'}, set(a.keys()))
791 791 nt.assert_equal(a['status'], 'ok')
792 792 data = a['data']
793 793 metadata = a['metadata']
794 794 nt.assert_equal(data.get('text/plain'), '3')
795 795
796 796 b = r['b']
797 797 nt.assert_equal(b['status'], 'error')
798 798 nt.assert_equal(b['ename'], 'ZeroDivisionError')
799 799
800 800 # back to text only
801 801 ip.display_formatter.active_types = ['text/plain']
802 802
803 803
804 804
805 805
806 806
807 807 class TestSyntaxErrorTransformer(unittest.TestCase):
808 808 """Check that SyntaxError raised by an input transformer is handled by run_cell()"""
809 809
810 810 class SyntaxErrorTransformer(InputTransformer):
811 811
812 812 def push(self, line):
813 813 pos = line.find('syntaxerror')
814 814 if pos >= 0:
815 815 e = SyntaxError('input contains "syntaxerror"')
816 816 e.text = line
817 817 e.offset = pos + 1
818 818 raise e
819 819 return line
820 820
821 821 def reset(self):
822 822 pass
823 823
824 824 def setUp(self):
825 825 self.transformer = TestSyntaxErrorTransformer.SyntaxErrorTransformer()
826 826 ip.input_splitter.python_line_transforms.append(self.transformer)
827 827 ip.input_transformer_manager.python_line_transforms.append(self.transformer)
828 828
829 829 def tearDown(self):
830 830 ip.input_splitter.python_line_transforms.remove(self.transformer)
831 831 ip.input_transformer_manager.python_line_transforms.remove(self.transformer)
832 832
833 833 def test_syntaxerror_input_transformer(self):
834 834 with tt.AssertPrints('1234'):
835 835 ip.run_cell('1234')
836 836 with tt.AssertPrints('SyntaxError: invalid syntax'):
837 837 ip.run_cell('1 2 3') # plain python syntax error
838 838 with tt.AssertPrints('SyntaxError: input contains "syntaxerror"'):
839 839 ip.run_cell('2345 # syntaxerror') # input transformer syntax error
840 840 with tt.AssertPrints('3456'):
841 841 ip.run_cell('3456')
842 842
843 843
844 844
845 845 def test_warning_suppression():
846 846 ip.run_cell("import warnings")
847 847 try:
848 848 with tt.AssertPrints("UserWarning: asdf", channel="stderr"):
849 849 ip.run_cell("warnings.warn('asdf')")
850 850 # Here's the real test -- if we run that again, we should get the
851 851 # warning again. Traditionally, each warning was only issued once per
852 852 # IPython session (approximately), even if the user typed in new and
853 853 # different code that should have also triggered the warning, leading
854 854 # to much confusion.
855 855 with tt.AssertPrints("UserWarning: asdf", channel="stderr"):
856 856 ip.run_cell("warnings.warn('asdf')")
857 857 finally:
858 858 ip.run_cell("del warnings")
859 859
860 860
861 861 def test_deprecation_warning():
862 862 ip.run_cell("""
863 863 import warnings
864 864 def wrn():
865 865 warnings.warn(
866 866 "I AM A WARNING",
867 867 DeprecationWarning
868 868 )
869 869 """)
870 870 try:
871 871 with tt.AssertPrints("I AM A WARNING", channel="stderr"):
872 872 ip.run_cell("wrn()")
873 873 finally:
874 874 ip.run_cell("del warnings")
875 875 ip.run_cell("del wrn")
876 876
877 877
878 878 class TestImportNoDeprecate(tt.TempFileMixin):
879 879
880 880 def setup(self):
881 881 """Make a valid python temp file."""
882 882 self.mktmp("""
883 883 import warnings
884 884 def wrn():
885 885 warnings.warn(
886 886 "I AM A WARNING",
887 887 DeprecationWarning
888 888 )
889 889 """)
890 890
891 891 def test_no_dep(self):
892 892 """
893 893 No deprecation warning should be raised from imported functions
894 894 """
895 895 ip.run_cell("from {} import wrn".format(self.fname))
896 896
897 897 with tt.AssertNotPrints("I AM A WARNING"):
898 898 ip.run_cell("wrn()")
899 899 ip.run_cell("del wrn")
General Comments 0
You need to be logged in to leave comments. Login now