##// END OF EJS Templates
Use SIMPLE_PROMPT while testing....
Matthias Bussonnier -
Show More
@@ -1,150 +1,151 b''
1 1 """Global IPython app to support test running.
2 2
3 3 We must start our own ipython object and heavily muck with it so that all the
4 4 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 8 from __future__ import absolute_import
9 9 from __future__ import print_function
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Copyright (C) 2009-2011 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.
16 16 #-----------------------------------------------------------------------------
17 17
18 18 #-----------------------------------------------------------------------------
19 19 # Imports
20 20 #-----------------------------------------------------------------------------
21 21
22 22 # stdlib
23 23 import sys
24 24
25 25 # our own
26 26 from . import tools
27 27
28 28 from IPython.core import page
29 29 from IPython.utils import io
30 30 from IPython.utils import py3compat
31 31 from IPython.utils.py3compat import builtin_mod
32 32 from IPython.terminal.interactiveshell import TerminalInteractiveShell
33 33
34 34 #-----------------------------------------------------------------------------
35 35 # Functions
36 36 #-----------------------------------------------------------------------------
37 37
38 38 class StreamProxy(io.IOStream):
39 39 """Proxy for sys.stdout/err. This will request the stream *at call time*
40 40 allowing for nose's Capture plugin's redirection of sys.stdout/err.
41 41
42 42 Parameters
43 43 ----------
44 44 name : str
45 45 The name of the stream. This will be requested anew at every call
46 46 """
47 47
48 48 def __init__(self, name):
49 49 self.name=name
50 50
51 51 @property
52 52 def stream(self):
53 53 return getattr(sys, self.name)
54 54
55 55 def flush(self):
56 56 self.stream.flush()
57 57
58 58
59 59 def get_ipython():
60 60 # This will get replaced by the real thing once we start IPython below
61 61 return start_ipython()
62 62
63 63
64 64 # A couple of methods to override those in the running IPython to interact
65 65 # better with doctest (doctest captures on raw stdout, so we need to direct
66 66 # various types of output there otherwise it will miss them).
67 67
68 68 def xsys(self, cmd):
69 69 """Replace the default system call with a capturing one for doctest.
70 70 """
71 71 # We use getoutput, but we need to strip it because pexpect captures
72 72 # the trailing newline differently from commands.getoutput
73 73 print(self.getoutput(cmd, split=False, depth=1).rstrip(), end='', file=sys.stdout)
74 74 sys.stdout.flush()
75 75
76 76
77 77 def _showtraceback(self, etype, evalue, stb):
78 78 """Print the traceback purely on stdout for doctest to capture it.
79 79 """
80 80 print(self.InteractiveTB.stb2text(stb), file=sys.stdout)
81 81
82 82
83 83 def start_ipython():
84 84 """Start a global IPython shell, which we need for IPython-specific syntax.
85 85 """
86 86 global get_ipython
87 87
88 88 # This function should only ever run once!
89 89 if hasattr(start_ipython, 'already_called'):
90 90 return
91 91 start_ipython.already_called = True
92 92
93 93 # Store certain global objects that IPython modifies
94 94 _displayhook = sys.displayhook
95 95 _excepthook = sys.excepthook
96 96 _main = sys.modules.get('__main__')
97 97
98 98 # Create custom argv and namespaces for our IPython to be test-friendly
99 99 config = tools.default_config()
100 config.TerminalInteractiveShell.simple_prompt = True
100 101
101 102 # Create and initialize our test-friendly IPython instance.
102 103 shell = TerminalInteractiveShell.instance(config=config,
103 104 )
104 105
105 106 # A few more tweaks needed for playing nicely with doctests...
106 107
107 108 # remove history file
108 109 shell.tempfiles.append(config.HistoryManager.hist_file)
109 110
110 111 # These traps are normally only active for interactive use, set them
111 112 # permanently since we'll be mocking interactive sessions.
112 113 shell.builtin_trap.activate()
113 114
114 115 # Modify the IPython system call with one that uses getoutput, so that we
115 116 # can capture subcommands and print them to Python's stdout, otherwise the
116 117 # doctest machinery would miss them.
117 118 shell.system = py3compat.MethodType(xsys, shell)
118 119
119 120 shell._showtraceback = py3compat.MethodType(_showtraceback, shell)
120 121
121 122 # IPython is ready, now clean up some global state...
122 123
123 124 # Deactivate the various python system hooks added by ipython for
124 125 # interactive convenience so we don't confuse the doctest system
125 126 sys.modules['__main__'] = _main
126 127 sys.displayhook = _displayhook
127 128 sys.excepthook = _excepthook
128 129
129 130 # So that ipython magics and aliases can be doctested (they work by making
130 131 # a call into a global _ip object). Also make the top-level get_ipython
131 132 # now return this without recursively calling here again.
132 133 _ip = shell
133 134 get_ipython = _ip.get_ipython
134 135 builtin_mod._ip = _ip
135 136 builtin_mod.get_ipython = get_ipython
136 137
137 138 # To avoid extra IPython messages during testing, suppress io.stdout/stderr
138 139 io.stdout = StreamProxy('stdout')
139 140 io.stderr = StreamProxy('stderr')
140 141
141 142 # Override paging, so we don't require user interaction during the tests.
142 143 def nopage(strng, start=0, screen_lines=0, pager_cmd=None):
143 144 if isinstance(strng, dict):
144 145 strng = strng.get('text/plain', '')
145 146 print(strng)
146 147
147 148 page.orig_page = page.pager_page
148 149 page.pager_page = nopage
149 150
150 151 return _ip
General Comments 0
You need to be logged in to leave comments. Login now