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