Show More
@@ -439,11 +439,13 b' class InteractiveShell(Configurable, Magic):' | |||
|
439 | 439 | self.object_info_string_level) |
|
440 | 440 | |
|
441 | 441 | def init_io(self): |
|
442 | import IPython.utils.io | |
|
442 | # This will just use sys.stdout and sys.stderr. If you want to | |
|
443 | # override sys.stdout and sys.stderr themselves, you need to do that | |
|
444 | # *before* instantiating this class, because Term holds onto | |
|
445 | # references to the underlying streams. | |
|
443 | 446 | if sys.platform == 'win32' and self.has_readline: |
|
444 | Term = io.IOTerm( | |
|
445 |
|
|
|
446 | ) | |
|
447 | Term = io.IOTerm(cout=self.readline._outputfile, | |
|
448 | cerr=self.readline._outputfile) | |
|
447 | 449 | else: |
|
448 | 450 | Term = io.IOTerm() |
|
449 | 451 | io.Term = Term |
@@ -1483,9 +1485,7 b' class InteractiveShell(Configurable, Magic):' | |||
|
1483 | 1485 | Subclasses may override this method to put the traceback on a different |
|
1484 | 1486 | place, like a side channel. |
|
1485 | 1487 | """ |
|
1486 | # FIXME: this should use the proper write channels, but our test suite | |
|
1487 | # relies on it coming out of stdout... | |
|
1488 | print >> sys.stdout, self.InteractiveTB.stb2text(stb) | |
|
1488 | print >> io.Term.cout, self.InteractiveTB.stb2text(stb) | |
|
1489 | 1489 | |
|
1490 | 1490 | def showsyntaxerror(self, filename=None): |
|
1491 | 1491 | """Display the syntax error that just occurred. |
@@ -5,11 +5,11 b" modifications IPython makes to system behavior don't send the doctest machinery" | |||
|
5 | 5 | into a fit. This code should be considered a gross hack, but it gets the job |
|
6 | 6 | done. |
|
7 | 7 | """ |
|
8 | ||
|
9 | 8 | from __future__ import absolute_import |
|
9 | from __future__ import print_function | |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | # Copyright (C) 2009 The IPython Development Team | |
|
12 | # Copyright (C) 2009-2010 The IPython Development Team | |
|
13 | 13 | # |
|
14 | 14 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | 15 | # the file COPYING, distributed as part of this software. |
@@ -19,13 +19,17 b' from __future__ import absolute_import' | |||
|
19 | 19 | # Imports |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | |
|
22 | # stdlib | |
|
22 | 23 | import __builtin__ |
|
23 | import commands | |
|
24 | 24 | import os |
|
25 | 25 | import sys |
|
26 | from types import MethodType | |
|
26 | 27 | |
|
28 | # our own | |
|
27 | 29 | from . import tools |
|
28 | 30 | |
|
31 | from IPython.frontend.terminal.interactiveshell import TerminalInteractiveShell | |
|
32 | ||
|
29 | 33 | #----------------------------------------------------------------------------- |
|
30 | 34 | # Functions |
|
31 | 35 | #----------------------------------------------------------------------------- |
@@ -53,12 +57,9 b' def _run_ns_sync(self,arg_s,runner=None):' | |||
|
53 | 57 | |
|
54 | 58 | This is strictly needed for running doctests that call %run. |
|
55 | 59 | """ |
|
56 |
#print |
|
|
57 | ||
|
58 | _ip = get_ipython() | |
|
60 | #print('in run_ns_sync', arg_s, file=sys.stderr) # dbg | |
|
59 | 61 | finder = py_file_finder(arg_s) |
|
60 |
|
|
|
61 | return out | |
|
62 | return get_ipython().magic_run_ori(arg_s, runner, finder) | |
|
62 | 63 | |
|
63 | 64 | |
|
64 | 65 | class ipnsdict(dict): |
@@ -104,6 +105,25 b' def get_ipython():' | |||
|
104 | 105 | return start_ipython() |
|
105 | 106 | |
|
106 | 107 | |
|
108 | # A couple of methods to override those in the running IPython to interact | |
|
109 | # better with doctest (doctest captures on raw stdout, so we need to direct | |
|
110 | # various types of output there otherwise it will miss them). | |
|
111 | ||
|
112 | def xsys(self, cmd): | |
|
113 | """Replace the default system call with a capturing one for doctest. | |
|
114 | """ | |
|
115 | # We use getoutput, but we need to strip it because pexpect captures | |
|
116 | # the trailing newline differently from commands.getoutput | |
|
117 | print(self.getoutput(cmd).rstrip(), end='', file=sys.stdout) | |
|
118 | sys.stdout.flush() | |
|
119 | ||
|
120 | ||
|
121 | def _showtraceback(self, etype, evalue, stb): | |
|
122 | """Print the traceback purely on stdout for doctest to capture it. | |
|
123 | """ | |
|
124 | print(self.InteractiveTB.stb2text(stb), file=sys.stdout) | |
|
125 | ||
|
126 | ||
|
107 | 127 | def start_ipython(): |
|
108 | 128 | """Start a global IPython shell, which we need for IPython-specific syntax. |
|
109 | 129 | """ |
@@ -113,19 +133,7 b' def start_ipython():' | |||
|
113 | 133 | if hasattr(start_ipython, 'already_called'): |
|
114 | 134 | return |
|
115 | 135 | start_ipython.already_called = True |
|
116 | ||
|
117 | from IPython.frontend.terminal import interactiveshell | |
|
118 | 136 | |
|
119 | def xsys(cmd): | |
|
120 | """Execute a command and print its output. | |
|
121 | ||
|
122 | This is just a convenience function to replace the IPython system call | |
|
123 | with one that is more doctest-friendly. | |
|
124 | """ | |
|
125 | cmd = _ip.var_expand(cmd,depth=1) | |
|
126 | sys.stdout.write(commands.getoutput(cmd)) | |
|
127 | sys.stdout.flush() | |
|
128 | ||
|
129 | 137 | # Store certain global objects that IPython modifies |
|
130 | 138 | _displayhook = sys.displayhook |
|
131 | 139 | _excepthook = sys.excepthook |
@@ -135,10 +143,10 b' def start_ipython():' | |||
|
135 | 143 | config = tools.default_config() |
|
136 | 144 | |
|
137 | 145 | # Create and initialize our test-friendly IPython instance. |
|
138 |
shell = |
|
|
139 | config=config, | |
|
140 | user_ns=ipnsdict(), user_global_ns={} | |
|
141 | ) | |
|
146 | shell = TerminalInteractiveShell.instance(config=config, | |
|
147 | user_ns=ipnsdict(), | |
|
148 | user_global_ns={} | |
|
149 | ) | |
|
142 | 150 | |
|
143 | 151 | # A few more tweaks needed for playing nicely with doctests... |
|
144 | 152 | |
@@ -149,7 +157,11 b' def start_ipython():' | |||
|
149 | 157 | # Modify the IPython system call with one that uses getoutput, so that we |
|
150 | 158 | # can capture subcommands and print them to Python's stdout, otherwise the |
|
151 | 159 | # doctest machinery would miss them. |
|
152 | shell.system = xsys | |
|
160 | shell.system = MethodType(xsys, shell, TerminalInteractiveShell) | |
|
161 | ||
|
162 | ||
|
163 | shell._showtraceback = MethodType(_showtraceback, shell, | |
|
164 | TerminalInteractiveShell) | |
|
153 | 165 | |
|
154 | 166 | # IPython is ready, now clean up some global state... |
|
155 | 167 |
@@ -27,6 +27,7 b' from IPython.core.interactiveshell import (' | |||
|
27 | 27 | from IPython.core.displayhook import DisplayHook |
|
28 | 28 | from IPython.core.macro import Macro |
|
29 | 29 | from IPython.core.payloadpage import install_payload_page |
|
30 | from IPython.utils import io | |
|
30 | 31 | from IPython.utils.path import get_py_filename |
|
31 | 32 | from IPython.utils.text import StringTypes |
|
32 | 33 | from IPython.utils.traitlets import Instance, Type, Dict |
@@ -77,15 +78,6 b' class ZMQInteractiveShell(InteractiveShell):' | |||
|
77 | 78 | |
|
78 | 79 | displayhook_class = Type(ZMQDisplayHook) |
|
79 | 80 | |
|
80 | def init_io(self): | |
|
81 | # This will just use sys.stdout and sys.stderr. If you want to | |
|
82 | # override sys.stdout and sys.stderr themselves, you need to do that | |
|
83 | # *before* instantiating this class, because Term holds onto | |
|
84 | # references to the underlying streams. | |
|
85 | import IPython.utils.io | |
|
86 | Term = IPython.utils.io.IOTerm() | |
|
87 | IPython.utils.io.Term = Term | |
|
88 | ||
|
89 | 81 | def magic_doctest_mode(self,parameter_s=''): |
|
90 | 82 | """Toggle doctest mode on and off. |
|
91 | 83 |
General Comments 0
You need to be logged in to leave comments.
Login now