Show More
@@ -33,13 +33,15 b' except ImportError:' | |||||
33 |
|
33 | |||
34 | # Our own packages |
|
34 | # Our own packages | |
35 | from IPython.core import debugger, oinspect |
|
35 | from IPython.core import debugger, oinspect | |
|
36 | from IPython.core import magic_arguments | |||
36 | from IPython.core import page |
|
37 | from IPython.core import page | |
37 | from IPython.core.error import UsageError |
|
38 | from IPython.core.error import UsageError | |
38 | from IPython.core.macro import Macro |
|
39 | from IPython.core.macro import Macro | |
39 | from IPython.core.magic import (Magics, magics_class, line_magic, |
|
40 | from IPython.core.magic import (Magics, magics_class, line_magic, cell_magic, | |
40 | line_cell_magic, on_off, needs_local_scope) |
|
41 | line_cell_magic, on_off, needs_local_scope) | |
41 | from IPython.testing.skipdoctest import skip_doctest |
|
42 | from IPython.testing.skipdoctest import skip_doctest | |
42 | from IPython.utils import py3compat |
|
43 | from IPython.utils import py3compat | |
|
44 | from IPython.utils.io import capture_output | |||
43 | from IPython.utils.ipstruct import Struct |
|
45 | from IPython.utils.ipstruct import Struct | |
44 | from IPython.utils.module_paths import find_mod |
|
46 | from IPython.utils.module_paths import find_mod | |
45 | from IPython.utils.path import get_py_filename, unquote_filename |
|
47 | from IPython.utils.path import get_py_filename, unquote_filename | |
@@ -988,3 +990,27 b' python-profiler package from non-free.""")' | |||||
988 | print 'Macro `%s` created. To execute, type its name (without quotes).' % name |
|
990 | print 'Macro `%s` created. To execute, type its name (without quotes).' % name | |
989 | print '=== Macro contents: ===' |
|
991 | print '=== Macro contents: ===' | |
990 | print macro, |
|
992 | print macro, | |
|
993 | ||||
|
994 | @magic_arguments.magic_arguments() | |||
|
995 | @magic_arguments.argument('-o', '--out', type=str, | |||
|
996 | help="""The name of the variable in which to store stdout | |||
|
997 | ||||
|
998 | If unspecified: stdout is discarded | |||
|
999 | """ | |||
|
1000 | ) | |||
|
1001 | @magic_arguments.argument('-e', '--err', type=str, | |||
|
1002 | help="""The name of the variable in which to store stderr | |||
|
1003 | ||||
|
1004 | If unspecified: stderr is discarded | |||
|
1005 | """ | |||
|
1006 | ) | |||
|
1007 | @cell_magic | |||
|
1008 | def capture(self, line, cell): | |||
|
1009 | """run the cell, capturing stdout/err""" | |||
|
1010 | args = magic_arguments.parse_argstring(self.capture, line) | |||
|
1011 | with capture_output() as io: | |||
|
1012 | self.shell.run_cell(cell) | |||
|
1013 | if args.out: | |||
|
1014 | self.shell.user_ns[args.out] = io.stdout | |||
|
1015 | if args.err: | |||
|
1016 | self.shell.user_ns[args.err] = io.stderr |
General Comments 0
You need to be logged in to leave comments.
Login now