Show More
@@ -1,221 +1,223 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | Frontend class that uses IPython0 to prefilter the inputs. |
|
3 | 3 | |
|
4 | 4 | Using the IPython0 mechanism gives us access to the magics. |
|
5 | 5 | |
|
6 | 6 | This is a transitory class, used here to do the transition between |
|
7 | 7 | ipython0 and ipython1. This class is meant to be short-lived as more |
|
8 | 8 | functionnality is abstracted out of ipython0 in reusable functions and |
|
9 | 9 | is added on the interpreter. This class can be a used to guide this |
|
10 | 10 | refactoring. |
|
11 | 11 | """ |
|
12 | 12 | __docformat__ = "restructuredtext en" |
|
13 | 13 | |
|
14 | 14 | #------------------------------------------------------------------------------- |
|
15 | 15 | # Copyright (C) 2008 The IPython Development Team |
|
16 | 16 | # |
|
17 | 17 | # Distributed under the terms of the BSD License. The full license is in |
|
18 | 18 | # the file COPYING, distributed as part of this software. |
|
19 | 19 | #------------------------------------------------------------------------------- |
|
20 | 20 | |
|
21 | 21 | #------------------------------------------------------------------------------- |
|
22 | 22 | # Imports |
|
23 | 23 | #------------------------------------------------------------------------------- |
|
24 | 24 | import sys |
|
25 | 25 | |
|
26 | 26 | from linefrontendbase import LineFrontEndBase, common_prefix |
|
27 | 27 | |
|
28 | 28 | from IPython.ipmaker import make_IPython |
|
29 | 29 | from IPython.ipapi import IPApi |
|
30 | 30 | from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap |
|
31 | 31 | |
|
32 | 32 | from IPython.kernel.core.sync_traceback_trap import SyncTracebackTrap |
|
33 | 33 | |
|
34 | 34 | from IPython.genutils import Term |
|
35 | 35 | import pydoc |
|
36 | 36 | import os |
|
37 | 37 | |
|
38 | 38 | |
|
39 | 39 | def mk_system_call(system_call_function, command): |
|
40 | 40 | """ given a os.system replacement, and a leading string command, |
|
41 | 41 | returns a function that will execute the command with the given |
|
42 | 42 | argument string. |
|
43 | 43 | """ |
|
44 | 44 | def my_system_call(args): |
|
45 | 45 | system_call_function("%s %s" % (command, args)) |
|
46 | 46 | return my_system_call |
|
47 | 47 | |
|
48 | 48 | #------------------------------------------------------------------------------- |
|
49 | 49 | # Frontend class using ipython0 to do the prefiltering. |
|
50 | 50 | #------------------------------------------------------------------------------- |
|
51 | 51 | class PrefilterFrontEnd(LineFrontEndBase): |
|
52 | 52 | """ Class that uses ipython0 to do prefilter the input, do the |
|
53 | 53 | completion and the magics. |
|
54 | 54 | |
|
55 | 55 | The core trick is to use an ipython0 instance to prefilter the |
|
56 | 56 | input, and share the namespace between the interpreter instance used |
|
57 | 57 | to execute the statements and the ipython0 used for code |
|
58 | 58 | completion... |
|
59 | 59 | """ |
|
60 | 60 | |
|
61 | 61 | def __init__(self, ipython0=None, *args, **kwargs): |
|
62 | 62 | """ Parameters: |
|
63 | 63 | ----------- |
|
64 | 64 | |
|
65 | 65 | ipython0: an optional ipython0 instance to use for command |
|
66 | 66 | prefiltering and completion. |
|
67 | 67 | """ |
|
68 | 68 | self.save_output_hooks() |
|
69 | 69 | if ipython0 is None: |
|
70 | 70 | # Instanciate an IPython0 interpreter to be able to use the |
|
71 | 71 | # prefiltering. |
|
72 | 72 | # XXX: argv=[] is a bit bold. |
|
73 | 73 | ipython0 = make_IPython(argv=[]) |
|
74 | 74 | self.ipython0 = ipython0 |
|
75 | 75 | # Set the pager: |
|
76 | 76 | self.ipython0.set_hook('show_in_pager', |
|
77 | 77 | lambda s, string: self.write("\n" + string)) |
|
78 | 78 | self.ipython0.write = self.write |
|
79 | 79 | self._ip = _ip = IPApi(self.ipython0) |
|
80 | 80 | # Make sure the raw system call doesn't get called, as we don't |
|
81 | 81 | # have a stdin accessible. |
|
82 | 82 | self._ip.system = self.system_call |
|
83 | 83 | # XXX: Muck around with magics so that they work better |
|
84 | 84 | # in our environment |
|
85 | 85 | self.ipython0.magic_ls = mk_system_call(self.system_call, |
|
86 | 86 | 'ls -CF') |
|
87 | 87 | # And now clean up the mess created by ipython0 |
|
88 | 88 | self.release_output() |
|
89 | 89 | if not 'banner' in kwargs and self.banner is None: |
|
90 | 90 | kwargs['banner'] = self.ipython0.BANNER + """ |
|
91 | 91 | This is the wx frontend, by Gael Varoquaux. This is EXPERIMENTAL code.""" |
|
92 | 92 | |
|
93 | 93 | LineFrontEndBase.__init__(self, *args, **kwargs) |
|
94 | 94 | # XXX: Hack: mix the two namespaces |
|
95 |
self.shell.user_ns |
|
|
96 | self.shell.user_global_ns = self.ipython0.user_global_ns | |
|
95 | self.shell.user_ns.update(self.ipython0.user_ns) | |
|
96 | self.ipython0.user_ns = self.shell.user_ns | |
|
97 | self.shell.user_global_ns.update(self.ipython0.user_global_ns) | |
|
98 | self.ipython0.user_global_ns = self.shell.user_global_ns | |
|
97 | 99 | |
|
98 | 100 | self.shell.output_trap = RedirectorOutputTrap( |
|
99 | 101 | out_callback=self.write, |
|
100 | 102 | err_callback=self.write, |
|
101 | 103 | ) |
|
102 | 104 | self.shell.traceback_trap = SyncTracebackTrap( |
|
103 | 105 | formatters=self.shell.traceback_trap.formatters, |
|
104 | 106 | ) |
|
105 | 107 | |
|
106 | 108 | #-------------------------------------------------------------------------- |
|
107 | 109 | # FrontEndBase interface |
|
108 | 110 | #-------------------------------------------------------------------------- |
|
109 | 111 | |
|
110 | 112 | def show_traceback(self): |
|
111 | 113 | """ Use ipython0 to capture the last traceback and display it. |
|
112 | 114 | """ |
|
113 | 115 | self.capture_output() |
|
114 | 116 | self.ipython0.showtraceback() |
|
115 | 117 | self.release_output() |
|
116 | 118 | |
|
117 | 119 | |
|
118 | 120 | def execute(self, python_string, raw_string=None): |
|
119 | 121 | if self.debug: |
|
120 | 122 | print 'Executing Python code:', repr(python_string) |
|
121 | 123 | self.capture_output() |
|
122 | 124 | LineFrontEndBase.execute(self, python_string, |
|
123 | 125 | raw_string=raw_string) |
|
124 | 126 | self.release_output() |
|
125 | 127 | |
|
126 | 128 | |
|
127 | 129 | def save_output_hooks(self): |
|
128 | 130 | """ Store all the output hooks we can think of, to be able to |
|
129 | 131 | restore them. |
|
130 | 132 | |
|
131 | 133 | We need to do this early, as starting the ipython0 instance will |
|
132 | 134 | screw ouput hooks. |
|
133 | 135 | """ |
|
134 | 136 | self.__old_cout_write = Term.cout.write |
|
135 | 137 | self.__old_cerr_write = Term.cerr.write |
|
136 | 138 | self.__old_stdout = sys.stdout |
|
137 | 139 | self.__old_stderr= sys.stderr |
|
138 | 140 | self.__old_help_output = pydoc.help.output |
|
139 | 141 | self.__old_display_hook = sys.displayhook |
|
140 | 142 | |
|
141 | 143 | |
|
142 | 144 | def capture_output(self): |
|
143 | 145 | """ Capture all the output mechanisms we can think of. |
|
144 | 146 | """ |
|
145 | 147 | self.save_output_hooks() |
|
146 | 148 | Term.cout.write = self.write |
|
147 | 149 | Term.cerr.write = self.write |
|
148 | 150 | sys.stdout = Term.cout |
|
149 | 151 | sys.stderr = Term.cerr |
|
150 | 152 | pydoc.help.output = self.shell.output_trap.out |
|
151 | 153 | |
|
152 | 154 | |
|
153 | 155 | def release_output(self): |
|
154 | 156 | """ Release all the different captures we have made. |
|
155 | 157 | """ |
|
156 | 158 | Term.cout.write = self.__old_cout_write |
|
157 | 159 | Term.cerr.write = self.__old_cerr_write |
|
158 | 160 | sys.stdout = self.__old_stdout |
|
159 | 161 | sys.stderr = self.__old_stderr |
|
160 | 162 | pydoc.help.output = self.__old_help_output |
|
161 | 163 | sys.displayhook = self.__old_display_hook |
|
162 | 164 | |
|
163 | 165 | |
|
164 | 166 | def complete(self, line): |
|
165 | 167 | word = line.split('\n')[-1].split(' ')[-1] |
|
166 | 168 | completions = self.ipython0.complete(word) |
|
167 | 169 | # FIXME: The proper sort should be done in the complete method. |
|
168 | 170 | key = lambda x: x.replace('_', '') |
|
169 | 171 | completions.sort(key=key) |
|
170 | 172 | if completions: |
|
171 | 173 | prefix = common_prefix(completions) |
|
172 | 174 | line = line[:-len(word)] + prefix |
|
173 | 175 | return line, completions |
|
174 | 176 | |
|
175 | 177 | |
|
176 | 178 | #-------------------------------------------------------------------------- |
|
177 | 179 | # LineFrontEndBase interface |
|
178 | 180 | #-------------------------------------------------------------------------- |
|
179 | 181 | |
|
180 | 182 | def prefilter_input(self, input_string): |
|
181 | 183 | """ Using IPython0 to prefilter the commands to turn them |
|
182 | 184 | in executable statements that are valid Python strings. |
|
183 | 185 | """ |
|
184 | 186 | input_string = LineFrontEndBase.prefilter_input(self, input_string) |
|
185 | 187 | filtered_lines = [] |
|
186 | 188 | # The IPython0 prefilters sometime produce output. We need to |
|
187 | 189 | # capture it. |
|
188 | 190 | self.capture_output() |
|
189 | 191 | self.last_result = dict(number=self.prompt_number) |
|
190 | 192 | try: |
|
191 | 193 | for line in input_string.split('\n'): |
|
192 | 194 | filtered_lines.append( |
|
193 | 195 | self.ipython0.prefilter(line, False).rstrip()) |
|
194 | 196 | except: |
|
195 | 197 | # XXX: probably not the right thing to do. |
|
196 | 198 | self.ipython0.showsyntaxerror() |
|
197 | 199 | self.after_execute() |
|
198 | 200 | finally: |
|
199 | 201 | self.release_output() |
|
200 | 202 | |
|
201 | 203 | # Clean up the trailing whitespace, to avoid indentation errors |
|
202 | 204 | filtered_string = '\n'.join(filtered_lines) |
|
203 | 205 | return filtered_string |
|
204 | 206 | |
|
205 | 207 | |
|
206 | 208 | #-------------------------------------------------------------------------- |
|
207 | 209 | # PrefilterFrontEnd interface |
|
208 | 210 | #-------------------------------------------------------------------------- |
|
209 | 211 | |
|
210 | 212 | def system_call(self, command_string): |
|
211 | 213 | """ Allows for frontend to define their own system call, to be |
|
212 | 214 | able capture output and redirect input. |
|
213 | 215 | """ |
|
214 | 216 | return os.system(command_string) |
|
215 | 217 | |
|
216 | 218 | |
|
217 | 219 | def do_exit(self): |
|
218 | 220 | """ Exit the shell, cleanup and save the history. |
|
219 | 221 | """ |
|
220 | 222 | self.ipython0.atexit_operations() |
|
221 | 223 |
@@ -1,157 +1,177 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | """ |
|
3 | 3 | Test process execution and IO redirection. |
|
4 | 4 | """ |
|
5 | 5 | |
|
6 | 6 | __docformat__ = "restructuredtext en" |
|
7 | 7 | |
|
8 | 8 | #------------------------------------------------------------------------------- |
|
9 | 9 | # Copyright (C) 2008 The IPython Development Team |
|
10 | 10 | # |
|
11 | 11 | # Distributed under the terms of the BSD License. The full license is |
|
12 | 12 | # in the file COPYING, distributed as part of this software. |
|
13 | 13 | #------------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | from cStringIO import StringIO |
|
16 | 16 | import string |
|
17 | 17 | |
|
18 | 18 | from IPython.ipapi import get as get_ipython0 |
|
19 | 19 | from IPython.frontend.prefilterfrontend import PrefilterFrontEnd |
|
20 | 20 | |
|
21 | 21 | class TestPrefilterFrontEnd(PrefilterFrontEnd): |
|
22 | 22 | |
|
23 | 23 | input_prompt_template = string.Template('') |
|
24 | 24 | output_prompt_template = string.Template('') |
|
25 | 25 | banner = '' |
|
26 | 26 | |
|
27 | 27 | def __init__(self): |
|
28 | 28 | ipython0 = get_ipython0().IP |
|
29 | 29 | self.out = StringIO() |
|
30 | 30 | PrefilterFrontEnd.__init__(self, ipython0=ipython0) |
|
31 | 31 | # Clean up the namespace for isolation between tests |
|
32 | 32 | user_ns = self.ipython0.user_ns |
|
33 | 33 | # We need to keep references to things so that they don't |
|
34 | 34 | # get garbage collected (this stinks). |
|
35 | 35 | self.shadow_ns = dict() |
|
36 | 36 | for i in self.ipython0.magic_who_ls(): |
|
37 | 37 | self.shadow_ns[i] = user_ns.pop(i) |
|
38 | 38 | # Some more code for isolation (yeah, crazy) |
|
39 | 39 | self._on_enter() |
|
40 | 40 | self.out.flush() |
|
41 | 41 | self.out.reset() |
|
42 | 42 | self.out.truncate() |
|
43 | 43 | |
|
44 | 44 | def write(self, string, *args, **kwargs): |
|
45 | 45 | self.out.write(string) |
|
46 | 46 | |
|
47 | 47 | def _on_enter(self): |
|
48 | 48 | self.input_buffer += '\n' |
|
49 | 49 | PrefilterFrontEnd._on_enter(self) |
|
50 | 50 | |
|
51 | 51 | |
|
52 | def isolate_ipython0(func): | |
|
53 | """ Decorator to isolate execution that involves an iptyhon0. | |
|
54 | """ | |
|
55 | def my_func(*args, **kwargs): | |
|
56 | ipython0 = get_ipython0().IP | |
|
57 | user_ns = ipython0.user_ns | |
|
58 | global_ns = ipython0.global_ns | |
|
59 | func(*args, **kwargs) | |
|
60 | ipython0.user_ns = user_ns | |
|
61 | ipython0.global_ns = global_ns | |
|
62 | ||
|
63 | return my_func | |
|
64 | ||
|
65 | ||
|
66 | @isolate_ipython0 | |
|
52 | 67 | def test_execution(): |
|
53 | 68 | """ Test execution of a command. |
|
54 | 69 | """ |
|
55 | 70 | f = TestPrefilterFrontEnd() |
|
56 | 71 | f.input_buffer = 'print 1' |
|
57 | 72 | f._on_enter() |
|
58 | 73 | out_value = f.out.getvalue() |
|
59 | 74 | assert out_value == '1\n' |
|
60 | 75 | |
|
61 | 76 | |
|
77 | @isolate_ipython0 | |
|
62 | 78 | def test_multiline(): |
|
63 | 79 | """ Test execution of a multiline command. |
|
64 | 80 | """ |
|
65 | 81 | f = TestPrefilterFrontEnd() |
|
66 | 82 | f.input_buffer = 'if True:' |
|
67 | 83 | f._on_enter() |
|
68 | 84 | f.input_buffer += 'print 1' |
|
69 | 85 | f._on_enter() |
|
70 | 86 | out_value = f.out.getvalue() |
|
71 | 87 | assert out_value == '' |
|
72 | 88 | f._on_enter() |
|
73 | 89 | out_value = f.out.getvalue() |
|
74 | 90 | assert out_value == '1\n' |
|
75 | 91 | f = TestPrefilterFrontEnd() |
|
76 | 92 | f.input_buffer='(1 +' |
|
77 | 93 | f._on_enter() |
|
78 | 94 | f.input_buffer += '0)' |
|
79 | 95 | f._on_enter() |
|
80 | 96 | out_value = f.out.getvalue() |
|
81 | 97 | assert out_value == '' |
|
82 | 98 | f._on_enter() |
|
83 | 99 | out_value = f.out.getvalue() |
|
84 | 100 | assert out_value == '1\n' |
|
85 | 101 | |
|
86 | 102 | |
|
103 | @isolate_ipython0 | |
|
87 | 104 | def test_capture(): |
|
88 | 105 | """ Test the capture of output in different channels. |
|
89 | 106 | """ |
|
90 | 107 | # Test on the OS-level stdout, stderr. |
|
91 | 108 | f = TestPrefilterFrontEnd() |
|
92 | 109 | f.input_buffer = \ |
|
93 | 110 | 'import os; out=os.fdopen(1, "w"); out.write("1") ; out.flush()' |
|
94 | 111 | f._on_enter() |
|
95 | 112 | out_value = f.out.getvalue() |
|
96 | 113 | assert out_value == '1' |
|
97 | 114 | f = TestPrefilterFrontEnd() |
|
98 | 115 | f.input_buffer = \ |
|
99 | 116 | 'import os; out=os.fdopen(2, "w"); out.write("1") ; out.flush()' |
|
100 | 117 | f._on_enter() |
|
101 | 118 | out_value = f.out.getvalue() |
|
102 | 119 | assert out_value == '1' |
|
103 | 120 | |
|
104 | 121 | |
|
122 | @isolate_ipython0 | |
|
105 | 123 | def test_magic(): |
|
106 | 124 | """ Test the magic expansion and history. |
|
107 | 125 | |
|
108 | 126 | This test is fairly fragile and will break when magics change. |
|
109 | 127 | """ |
|
110 | 128 | f = TestPrefilterFrontEnd() |
|
111 | 129 | f.input_buffer += '%who' |
|
112 | 130 | f._on_enter() |
|
113 | 131 | out_value = f.out.getvalue() |
|
114 | 132 | assert out_value == 'Interactive namespace is empty.\n' |
|
115 | 133 | |
|
116 | 134 | |
|
135 | @isolate_ipython0 | |
|
117 | 136 | def test_help(): |
|
118 | 137 | """ Test object inspection. |
|
119 | 138 | """ |
|
120 | 139 | f = TestPrefilterFrontEnd() |
|
121 | 140 | f.input_buffer += "def f():" |
|
122 | 141 | f._on_enter() |
|
123 | 142 | f.input_buffer += "'foobar'" |
|
124 | 143 | f._on_enter() |
|
125 | 144 | f.input_buffer += "pass" |
|
126 | 145 | f._on_enter() |
|
127 | 146 | f._on_enter() |
|
128 | 147 | f.input_buffer += "f?" |
|
129 | 148 | f._on_enter() |
|
130 | 149 | assert 'traceback' not in f.last_result |
|
131 | 150 | ## XXX: ipython doctest magic breaks this. I have no clue why |
|
132 | 151 | #out_value = f.out.getvalue() |
|
133 | 152 | #assert out_value.split()[-1] == 'foobar' |
|
134 | 153 | |
|
135 | 154 | |
|
155 | @isolate_ipython0 | |
|
136 | 156 | def test_completion(): |
|
137 | 157 | """ Test command-line completion. |
|
138 | 158 | """ |
|
139 | 159 | f = TestPrefilterFrontEnd() |
|
140 | 160 | f.input_buffer = 'zzza = 1' |
|
141 | 161 | f._on_enter() |
|
142 | 162 | f.input_buffer = 'zzzb = 2' |
|
143 | 163 | f._on_enter() |
|
144 | 164 | f.input_buffer = 'zz' |
|
145 | 165 | f.complete_current_input() |
|
146 | 166 | out_value = f.out.getvalue() |
|
147 | 167 | assert out_value == '\nzzza zzzb ' |
|
148 | 168 | assert f.input_buffer == 'zzz' |
|
149 | 169 | |
|
150 | 170 | |
|
151 | 171 | if __name__ == '__main__': |
|
152 | 172 | test_magic() |
|
153 | 173 | test_help() |
|
154 | 174 | test_execution() |
|
155 | 175 | test_multiline() |
|
156 | 176 | test_capture() |
|
157 | 177 | test_completion() |
General Comments 0
You need to be logged in to leave comments.
Login now