Show More
@@ -1,12 +1,11 b'' | |||
|
1 | 1 | """Analysis of text input into executable blocks. |
|
2 | 2 | |
|
3 | This is a simple example of how an interactive terminal-based client can use | |
|
4 | this tool:: | |
|
3 | The main class in this module, :class:`InputSplitter`, is designed to break | |
|
4 | input from either interactive, line-by-line environments or block-based ones, | |
|
5 | into standalone blocks that can be executed by Python as 'single' statements | |
|
6 | (thus triggering sys.displayhook). | |
|
5 | 7 | |
|
6 | bb = BlockBreaker() | |
|
7 | while not bb.interactive_block_ready(): | |
|
8 | bb.push(raw_input('>>> ')) | |
|
9 | print 'Input source was:\n', bb.source, | |
|
8 | For more details, see the class docstring below. | |
|
10 | 9 | """ |
|
11 | 10 | #----------------------------------------------------------------------------- |
|
12 | 11 | # Copyright (C) 2010 The IPython Development Team |
@@ -43,6 +42,10 b' def num_ini_spaces(s):' | |||
|
43 | 42 | Parameters |
|
44 | 43 | ---------- |
|
45 | 44 | s : string |
|
45 | ||
|
46 | Returns | |
|
47 | ------- | |
|
48 | n : int | |
|
46 | 49 | """ |
|
47 | 50 | |
|
48 | 51 | ini_spaces = ini_spaces_re.match(s) |
@@ -78,31 +81,64 b' def get_input_encoding():' | |||
|
78 | 81 | # Classes and functions |
|
79 | 82 | #----------------------------------------------------------------------------- |
|
80 | 83 | |
|
81 |
class |
|
|
82 | # Command compiler | |
|
83 | compile = None | |
|
84 | # Number of spaces of indentation | |
|
84 | class InputSplitter(object): | |
|
85 | """An object that can split Python source input in executable blocks. | |
|
86 | ||
|
87 | This object is designed to be used in one of two basic modes: | |
|
88 | ||
|
89 | 1. By feeding it python source line-by-line, using :meth:`push`. In this | |
|
90 | mode, it will return on each push whether the currently pushed code | |
|
91 | could be executed already. In addition, it provides a method called | |
|
92 | :meth:`push_accepts_more` that can be used to query whether more input | |
|
93 | can be pushed into a single interactive block. | |
|
94 | ||
|
95 | 2. By calling :meth:`split_blocks` with a single, multiline Python string, | |
|
96 | that is then split into blocks each of which can be executed | |
|
97 | interactively as a single statement. | |
|
98 | ||
|
99 | This is a simple example of how an interactive terminal-based client can use | |
|
100 | this tool:: | |
|
101 | ||
|
102 | isp = InputSplitter() | |
|
103 | while isp.push_accepts_more(): | |
|
104 | indent = ' '*isp.indent_spaces | |
|
105 | prompt = '>>> ' + indent | |
|
106 | line = indent + raw_input(prompt) | |
|
107 | isp.push(line) | |
|
108 | print 'Input source was:\n', isp.source_reset(), | |
|
109 | """ | |
|
110 | # Number of spaces of indentation computed from input that has been pushed | |
|
111 | # so far. This is the attributes callers should query to get the current | |
|
112 | # indentation level, in order to provide auto-indent facilities. | |
|
85 | 113 | indent_spaces = 0 |
|
86 | # Mark when input has changed indentation all the way back to flush-left | |
|
87 | full_dedent = False | |
|
88 | # String, indicating the default input encoding | |
|
114 | # String, indicating the default input encoding. It is computed by default | |
|
115 | # at initialization time via get_input_encoding(), but it can be reset by a | |
|
116 | # client with specific knowledge of the encoding. | |
|
89 | 117 | encoding = '' |
|
90 | # String where the current full source input is stored, properly encoded | |
|
118 | # String where the current full source input is stored, properly encoded. | |
|
119 | # Reading this attribute is the normal way of querying the currently pushed | |
|
120 | # source code, that has been properly encoded. | |
|
91 | 121 | source = '' |
|
92 | # Code object corresponding to the current source | |
|
122 | # Code object corresponding to the current source. It is automatically | |
|
123 | # synced to the source, so it can be queried at any time to obtain the code | |
|
124 | # object; it will be None if the source doesn't compile to valid Python. | |
|
93 | 125 | code = None |
|
94 | # Boolean indicating whether the current block is complete | |
|
95 | is_complete = None | |
|
96 | 126 | # Input mode |
|
97 | 127 | input_mode = 'append' |
|
98 | 128 | |
|
99 | 129 | # Private attributes |
|
100 | 130 | |
|
101 | # List | |
|
131 | # List with lines of input accumulated so far | |
|
102 | 132 | _buffer = None |
|
133 | # Command compiler | |
|
134 | _compile = None | |
|
135 | # Mark when input has changed indentation all the way back to flush-left | |
|
136 | _full_dedent = False | |
|
137 | # Boolean indicating whether the current block is complete | |
|
138 | _is_complete = None | |
|
103 | 139 | |
|
104 | 140 | def __init__(self, input_mode=None): |
|
105 |
"""Create a new |
|
|
141 | """Create a new InputSplitter instance. | |
|
106 | 142 | |
|
107 | 143 | Parameters |
|
108 | 144 | ---------- |
@@ -118,9 +154,9 b' class BlockBreaker(object):' | |||
|
118 | 154 | while block-oriented ones will want to use 'replace'. |
|
119 | 155 | """ |
|
120 | 156 | self._buffer = [] |
|
121 | self.compile = codeop.CommandCompiler() | |
|
157 | self._compile = codeop.CommandCompiler() | |
|
122 | 158 | self.encoding = get_input_encoding() |
|
123 |
self.input_mode = |
|
|
159 | self.input_mode = InputSplitter.input_mode if input_mode is None \ | |
|
124 | 160 | else input_mode |
|
125 | 161 | |
|
126 | 162 | def reset(self): |
@@ -129,8 +165,8 b' class BlockBreaker(object):' | |||
|
129 | 165 | self._buffer[:] = [] |
|
130 | 166 | self.source = '' |
|
131 | 167 | self.code = None |
|
132 | self.is_complete = False | |
|
133 | self.full_dedent = False | |
|
168 | self._is_complete = False | |
|
169 | self._full_dedent = False | |
|
134 | 170 | |
|
135 | 171 | def source_reset(self): |
|
136 | 172 | """Return the input source and perform a full reset. |
@@ -145,7 +181,8 b' class BlockBreaker(object):' | |||
|
145 | 181 | This stores the given lines and returns a status code indicating |
|
146 | 182 | whether the code forms a complete Python block or not. |
|
147 | 183 | |
|
148 |
Any exceptions generated in compilation are allowed |
|
|
184 | Any exceptions generated in compilation are swallowed, but if an | |
|
185 | exception was produced, the method returns True. | |
|
149 | 186 | |
|
150 | 187 | Parameters |
|
151 | 188 | ---------- |
@@ -157,8 +194,8 b' class BlockBreaker(object):' | |||
|
157 | 194 | is_complete : boolean |
|
158 | 195 | True if the current input source (the result of the current input |
|
159 | 196 | plus prior inputs) forms a complete Python execution block. Note that |
|
160 |
this value is also stored as a |
|
|
161 | time. | |
|
197 | this value is also stored as a private attribute (_is_complete), so it | |
|
198 | can be queried at any time. | |
|
162 | 199 | """ |
|
163 | 200 | if self.input_mode == 'replace': |
|
164 | 201 | self.reset() |
@@ -173,14 +210,14 b' class BlockBreaker(object):' | |||
|
173 | 210 | self._store(lines) |
|
174 | 211 | source = self.source |
|
175 | 212 | |
|
176 | # Before calling compile(), reset the code object to None so that if an | |
|
213 | # Before calling _compile(), reset the code object to None so that if an | |
|
177 | 214 | # exception is raised in compilation, we don't mislead by having |
|
178 | 215 | # inconsistent code/source attributes. |
|
179 | self.code, self.is_complete = None, None | |
|
216 | self.code, self._is_complete = None, None | |
|
180 | 217 | |
|
181 | 218 | self._update_indent(lines) |
|
182 | 219 | try: |
|
183 | self.code = self.compile(source) | |
|
220 | self.code = self._compile(source) | |
|
184 | 221 | # Invalid syntax can produce any of a number of different errors from |
|
185 | 222 | # inside the compiler, so we have to catch them all. Syntax errors |
|
186 | 223 | # immediately produce a 'ready' block, so the invalid Python can be |
@@ -188,21 +225,22 b' class BlockBreaker(object):' | |||
|
188 | 225 | # special-syntax conversion. |
|
189 | 226 | except (SyntaxError, OverflowError, ValueError, TypeError, |
|
190 | 227 | MemoryError): |
|
191 | self.is_complete = True | |
|
228 | self._is_complete = True | |
|
192 | 229 | else: |
|
193 | 230 | # Compilation didn't produce any exceptions (though it may not have |
|
194 | 231 | # given a complete code object) |
|
195 | self.is_complete = self.code is not None | |
|
232 | self._is_complete = self.code is not None | |
|
196 | 233 | |
|
197 | return self.is_complete | |
|
234 | return self._is_complete | |
|
198 | 235 | |
|
199 |
def |
|
|
200 |
"""Return whether a block of interactive input |
|
|
236 | def push_accepts_more(self): | |
|
237 | """Return whether a block of interactive input can accept more input. | |
|
201 | 238 | |
|
202 | 239 | This method is meant to be used by line-oriented frontends, who need to |
|
203 | 240 | guess whether a block is complete or not based solely on prior and |
|
204 |
current input lines. The |
|
|
205 | interactive block when *all* of the following are true: | |
|
241 | current input lines. The InputSplitter considers it has a complete | |
|
242 | interactive block and will not accept more input only when either a | |
|
243 | SyntaxError is raised, or *all* of the following are true: | |
|
206 | 244 | |
|
207 | 245 | 1. The input compiles to a complete statement. |
|
208 | 246 | |
@@ -218,21 +256,23 b' class BlockBreaker(object):' | |||
|
218 | 256 | |
|
219 | 257 | Block-oriented frontends that have a separate keyboard event to |
|
220 | 258 | indicate execution should use the :meth:`split_blocks` method instead. |
|
259 | ||
|
260 | If the current input produces a syntax error, this method immediately | |
|
261 | returns False but does *not* raise the syntax error exception, as | |
|
262 | typically clients will want to send invalid syntax to an execution | |
|
263 | backend which might convert the invalid syntax into valid Python via | |
|
264 | one of the dynamic IPython mechanisms. | |
|
221 | 265 | """ |
|
222 | #print 'complete?', self.source # dbg | |
|
223 | #if self.full_dedent: | |
|
224 | # True | |
|
225 | 266 | |
|
226 | if not self.is_complete: | |
|
227 | return False | |
|
228 | if self.indent_spaces==0: | |
|
267 | if not self._is_complete: | |
|
229 | 268 | return True |
|
230 | last_line = self.source.splitlines()[-1] | |
|
231 | if not last_line or last_line.isspace(): | |
|
232 | return True | |
|
233 | else: | |
|
234 | return False | |
|
235 | 269 | |
|
270 | if self.indent_spaces==0: | |
|
271 | return False | |
|
272 | ||
|
273 | last_line = self.source.splitlines()[-1] | |
|
274 | return bool(last_line and not last_line.isspace()) | |
|
275 | ||
|
236 | 276 | def split_blocks(self, lines): |
|
237 | 277 | """Split a multiline string into multiple input blocks. |
|
238 | 278 | |
@@ -275,7 +315,7 b' class BlockBreaker(object):' | |||
|
275 | 315 | continue |
|
276 | 316 | |
|
277 | 317 | # Check indentation changes caused by the *next* line |
|
278 | indent_spaces, full_dedent = self._find_indent(next_line) | |
|
318 | indent_spaces, _full_dedent = self._find_indent(next_line) | |
|
279 | 319 | |
|
280 | 320 | # If the next line causes a dedent, it can be for two differnt |
|
281 | 321 | # reasons: either an explicit de-dent by the user or a |
@@ -292,7 +332,7 b' class BlockBreaker(object):' | |||
|
292 | 332 | # to start a new block. |
|
293 | 333 | |
|
294 | 334 | # Case 1, explicit dedent causes a break |
|
295 | if full_dedent and not next_line.startswith(' '): | |
|
335 | if _full_dedent and not next_line.startswith(' '): | |
|
296 | 336 | lines.append(next_line) |
|
297 | 337 | break |
|
298 | 338 | |
@@ -300,8 +340,8 b' class BlockBreaker(object):' | |||
|
300 | 340 | self.push(next_line) |
|
301 | 341 | |
|
302 | 342 | # Case 2, full dedent with full block ready: |
|
303 | if full_dedent or \ | |
|
304 |
self.indent_spaces==0 and self. |
|
|
343 | if _full_dedent or \ | |
|
344 | self.indent_spaces==0 and not self.push_accepts_more(): | |
|
305 | 345 | break |
|
306 | 346 | # Form the new block with the current source input |
|
307 | 347 | blocks.append(self.source_reset()) |
@@ -330,7 +370,7 b' class BlockBreaker(object):' | |||
|
330 | 370 | Whether the new line causes a full flush-left dedent. |
|
331 | 371 | """ |
|
332 | 372 | indent_spaces = self.indent_spaces |
|
333 | full_dedent = self.full_dedent | |
|
373 | full_dedent = self._full_dedent | |
|
334 | 374 | |
|
335 | 375 | inisp = num_ini_spaces(line) |
|
336 | 376 | if inisp < indent_spaces: |
@@ -356,7 +396,7 b' class BlockBreaker(object):' | |||
|
356 | 396 | def _update_indent(self, lines): |
|
357 | 397 | for line in remove_comments(lines).splitlines(): |
|
358 | 398 | if line and not line.isspace(): |
|
359 | self.indent_spaces, self.full_dedent = self._find_indent(line) | |
|
399 | self.indent_spaces, self._full_dedent = self._find_indent(line) | |
|
360 | 400 | |
|
361 | 401 | def _store(self, lines): |
|
362 | 402 | """Store one or more lines of input. |
@@ -372,4 +412,3 b' class BlockBreaker(object):' | |||
|
372 | 412 | |
|
373 | 413 | def _set_source(self): |
|
374 | 414 | self.source = ''.join(self._buffer).encode(self.encoding) |
|
375 |
@@ -1,4 +1,4 b'' | |||
|
1 |
"""Tests for the |
|
|
1 | """Tests for the inputsplitter module. | |
|
2 | 2 | """ |
|
3 | 3 | #----------------------------------------------------------------------------- |
|
4 | 4 | # Copyright (C) 2010 The IPython Development Team |
@@ -17,7 +17,34 b' import unittest' | |||
|
17 | 17 | import nose.tools as nt |
|
18 | 18 | |
|
19 | 19 | # Our own |
|
20 |
from IPython.core import |
|
|
20 | from IPython.core import inputsplitter as isp | |
|
21 | ||
|
22 | #----------------------------------------------------------------------------- | |
|
23 | # Semi-complete examples (also used as tests) | |
|
24 | #----------------------------------------------------------------------------- | |
|
25 | def mini_interactive_loop(raw_input): | |
|
26 | """Minimal example of the logic of an interactive interpreter loop. | |
|
27 | ||
|
28 | This serves as an example, and it is used by the test system with a fake | |
|
29 | raw_input that simulates interactive input.""" | |
|
30 | ||
|
31 | from IPython.core.inputsplitter import InputSplitter | |
|
32 | ||
|
33 | isp = InputSplitter() | |
|
34 | # In practice, this input loop would be wrapped in an outside loop to read | |
|
35 | # input indefinitely, until some exit/quit command was issued. Here we | |
|
36 | # only illustrate the basic inner loop. | |
|
37 | while isp.push_accepts_more(): | |
|
38 | indent = ' '*isp.indent_spaces | |
|
39 | prompt = '>>> ' + indent | |
|
40 | line = indent + raw_input(prompt) | |
|
41 | isp.push(line) | |
|
42 | ||
|
43 | # Here we just return input so we can use it in a test suite, but a real | |
|
44 | # interpreter would instead send it for execution somewhere. | |
|
45 | src = isp.source_reset() | |
|
46 | print 'Input source was:\n', src | |
|
47 | return src | |
|
21 | 48 | |
|
22 | 49 | #----------------------------------------------------------------------------- |
|
23 | 50 | # Test utilities, just for local use |
@@ -27,6 +54,17 b' def assemble(block):' | |||
|
27 | 54 | """Assemble a block into multi-line sub-blocks.""" |
|
28 | 55 | return ['\n'.join(sub_block)+'\n' for sub_block in block] |
|
29 | 56 | |
|
57 | ||
|
58 | def pseudo_input(lines): | |
|
59 | """Return a function that acts like raw_input but feeds the input list.""" | |
|
60 | ilines = iter(lines) | |
|
61 | def raw_in(prompt): | |
|
62 | try: | |
|
63 | return next(ilines) | |
|
64 | except StopIteration: | |
|
65 | return '' | |
|
66 | return raw_in | |
|
67 | ||
|
30 | 68 | #----------------------------------------------------------------------------- |
|
31 | 69 | # Tests |
|
32 | 70 | #----------------------------------------------------------------------------- |
@@ -45,7 +83,7 b' def test_spaces():' | |||
|
45 | 83 | ] |
|
46 | 84 | |
|
47 | 85 | for s, nsp in tests: |
|
48 |
nt.assert_equal( |
|
|
86 | nt.assert_equal(isp.num_ini_spaces(s), nsp) | |
|
49 | 87 | |
|
50 | 88 | |
|
51 | 89 | def test_remove_comments(): |
@@ -59,120 +97,117 b' def test_remove_comments():' | |||
|
59 | 97 | ] |
|
60 | 98 | |
|
61 | 99 | for inp, out in tests: |
|
62 |
nt.assert_equal( |
|
|
100 | nt.assert_equal(isp.remove_comments(inp), out) | |
|
63 | 101 | |
|
64 | 102 | |
|
65 | 103 | def test_get_input_encoding(): |
|
66 |
encoding = |
|
|
104 | encoding = isp.get_input_encoding() | |
|
67 | 105 | nt.assert_true(isinstance(encoding, basestring)) |
|
68 | 106 | # simple-minded check that at least encoding a simple string works with the |
|
69 | 107 | # encoding we got. |
|
70 | 108 | nt.assert_equal('test'.encode(encoding), 'test') |
|
71 | 109 | |
|
72 | 110 | |
|
73 |
class |
|
|
111 | class InputSplitterTestCase(unittest.TestCase): | |
|
74 | 112 | def setUp(self): |
|
75 |
self. |
|
|
113 | self.isp = isp.InputSplitter() | |
|
76 | 114 | |
|
77 | 115 | def test_reset(self): |
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
self.assertEqual( |
|
|
82 |
self.assertEqual( |
|
|
83 |
self.assertEqual( |
|
|
84 |
self.assertEqual( |
|
|
85 |
self.assertEqual( |
|
|
116 | isp = self.isp | |
|
117 | isp.push('x=1') | |
|
118 | isp.reset() | |
|
119 | self.assertEqual(isp._buffer, []) | |
|
120 | self.assertEqual(isp.indent_spaces, 0) | |
|
121 | self.assertEqual(isp.source, '') | |
|
122 | self.assertEqual(isp.code, None) | |
|
123 | self.assertEqual(isp._is_complete, False) | |
|
86 | 124 | |
|
87 | 125 | def test_source(self): |
|
88 |
self. |
|
|
89 |
self. |
|
|
90 |
self.assertEqual(self. |
|
|
91 |
self.assertTrue(len(self. |
|
|
92 |
self.assertEqual(self. |
|
|
93 |
self.assertEqual(self. |
|
|
94 |
self.assertEqual(self. |
|
|
126 | self.isp._store('1') | |
|
127 | self.isp._store('2') | |
|
128 | self.assertEqual(self.isp.source, '1\n2\n') | |
|
129 | self.assertTrue(len(self.isp._buffer)>0) | |
|
130 | self.assertEqual(self.isp.source_reset(), '1\n2\n') | |
|
131 | self.assertEqual(self.isp._buffer, []) | |
|
132 | self.assertEqual(self.isp.source, '') | |
|
95 | 133 | |
|
96 | 134 | def test_indent(self): |
|
97 |
|
|
|
98 |
|
|
|
99 |
self.assertEqual( |
|
|
100 |
|
|
|
101 |
self.assertEqual( |
|
|
102 |
|
|
|
103 |
self.assertEqual( |
|
|
104 |
|
|
|
105 |
self.assertEqual( |
|
|
106 |
|
|
|
107 |
self.assertEqual( |
|
|
135 | isp = self.isp # shorthand | |
|
136 | isp.push('x=1') | |
|
137 | self.assertEqual(isp.indent_spaces, 0) | |
|
138 | isp.push('if 1:\n x=1') | |
|
139 | self.assertEqual(isp.indent_spaces, 4) | |
|
140 | isp.push('y=2\n') | |
|
141 | self.assertEqual(isp.indent_spaces, 0) | |
|
142 | isp.push('if 1:') | |
|
143 | self.assertEqual(isp.indent_spaces, 4) | |
|
144 | isp.push(' x=1') | |
|
145 | self.assertEqual(isp.indent_spaces, 4) | |
|
108 | 146 | # Blank lines shouldn't change the indent level |
|
109 |
|
|
|
110 |
self.assertEqual( |
|
|
147 | isp.push(' '*2) | |
|
148 | self.assertEqual(isp.indent_spaces, 4) | |
|
111 | 149 | |
|
112 | 150 | def test_indent2(self): |
|
113 |
|
|
|
151 | isp = self.isp | |
|
114 | 152 | # When a multiline statement contains parens or multiline strings, we |
|
115 | 153 | # shouldn't get confused. |
|
116 |
|
|
|
117 |
|
|
|
118 |
self.assertEqual( |
|
|
154 | isp.push("if 1:") | |
|
155 | isp.push(" x = (1+\n 2)") | |
|
156 | self.assertEqual(isp.indent_spaces, 4) | |
|
119 | 157 | |
|
120 | 158 | def test_dedent(self): |
|
121 |
|
|
|
122 |
|
|
|
123 |
self.assertEqual( |
|
|
124 |
|
|
|
125 |
self.assertEqual( |
|
|
159 | isp = self.isp # shorthand | |
|
160 | isp.push('if 1:') | |
|
161 | self.assertEqual(isp.indent_spaces, 4) | |
|
162 | isp.push(' pass') | |
|
163 | self.assertEqual(isp.indent_spaces, 0) | |
|
126 | 164 | |
|
127 | 165 | def test_push(self): |
|
128 |
|
|
|
129 |
|
|
|
130 | self.assertTrue(bb.is_complete) | |
|
166 | isp = self.isp | |
|
167 | self.assertTrue(isp.push('x=1')) | |
|
131 | 168 | |
|
132 | 169 | def test_push2(self): |
|
133 |
|
|
|
134 |
|
|
|
135 | self.assertFalse(bb.is_complete) | |
|
170 | isp = self.isp | |
|
171 | self.assertFalse(isp.push('if 1:')) | |
|
136 | 172 | for line in [' x=1', '# a comment', ' y=2']: |
|
137 |
|
|
|
138 | self.assertTrue(bb.is_complete) | |
|
173 | self.assertTrue(isp.push(line)) | |
|
139 | 174 | |
|
140 | 175 | def test_push3(self): |
|
141 | 176 | """Test input with leading whitespace""" |
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
self.assertEqual( |
|
|
177 | isp = self.isp | |
|
178 | isp.push(' x=1') | |
|
179 | isp.push(' y=2') | |
|
180 | self.assertEqual(isp.source, 'if 1:\n x=1\n y=2\n') | |
|
146 | 181 | |
|
147 | 182 | def test_replace_mode(self): |
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
self.assertEqual( |
|
|
152 |
|
|
|
153 |
self.assertEqual( |
|
|
154 | ||
|
155 |
def test_ |
|
|
156 |
|
|
|
157 |
|
|
|
158 |
self.assert |
|
|
159 | ||
|
160 |
def test_ |
|
|
161 |
|
|
|
162 |
|
|
|
163 |
self.assert |
|
|
164 |
|
|
|
165 |
self.assert |
|
|
166 |
|
|
|
167 |
self.assert |
|
|
183 | isp = self.isp | |
|
184 | isp.input_mode = 'replace' | |
|
185 | isp.push('x=1') | |
|
186 | self.assertEqual(isp.source, 'x=1\n') | |
|
187 | isp.push('x=2') | |
|
188 | self.assertEqual(isp.source, 'x=2\n') | |
|
189 | ||
|
190 | def test_push_accepts_more(self): | |
|
191 | isp = self.isp | |
|
192 | isp.push('x=1') | |
|
193 | self.assertFalse(isp.push_accepts_more()) | |
|
194 | ||
|
195 | def test_push_accepts_more2(self): | |
|
196 | isp = self.isp | |
|
197 | isp.push('if 1:') | |
|
198 | self.assertTrue(isp.push_accepts_more()) | |
|
199 | isp.push(' x=1') | |
|
200 | self.assertTrue(isp.push_accepts_more()) | |
|
201 | isp.push('') | |
|
202 | self.assertFalse(isp.push_accepts_more()) | |
|
168 | 203 | |
|
169 |
def test_ |
|
|
170 |
|
|
|
171 |
|
|
|
172 |
self.assert |
|
|
204 | def test_push_accepts_more3(self): | |
|
205 | isp = self.isp | |
|
206 | isp.push("x = (2+\n3)") | |
|
207 | self.assertFalse(isp.push_accepts_more()) | |
|
173 | 208 | |
|
174 |
def test_ |
|
|
175 |
|
|
|
209 | def test_push_accepts_more4(self): | |
|
210 | isp = self.isp | |
|
176 | 211 | # When a multiline statement contains parens or multiline strings, we |
|
177 | 212 | # shouldn't get confused. |
|
178 | 213 | # FIXME: we should be able to better handle de-dents in statements like |
@@ -180,31 +215,31 b' class BlockBreakerTestCase(unittest.TestCase):' | |||
|
180 | 215 | # parens). Right now we aren't handling the indentation tracking quite |
|
181 | 216 | # correctly with this, though in practice it may not be too much of a |
|
182 | 217 | # problem. We'll need to see. |
|
183 |
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
self.assert |
|
|
187 |
|
|
|
188 |
self.assert |
|
|
189 |
|
|
|
190 |
self.assert |
|
|
218 | isp.push("if 1:") | |
|
219 | isp.push(" x = (2+") | |
|
220 | isp.push(" 3)") | |
|
221 | self.assertTrue(isp.push_accepts_more()) | |
|
222 | isp.push(" y = 3") | |
|
223 | self.assertTrue(isp.push_accepts_more()) | |
|
224 | isp.push('') | |
|
225 | self.assertFalse(isp.push_accepts_more()) | |
|
191 | 226 | |
|
192 | 227 | def test_syntax_error(self): |
|
193 |
|
|
|
228 | isp = self.isp | |
|
194 | 229 | # Syntax errors immediately produce a 'ready' block, so the invalid |
|
195 | 230 | # Python can be sent to the kernel for evaluation with possible ipython |
|
196 | 231 | # special-syntax conversion. |
|
197 |
|
|
|
198 |
self.assert |
|
|
232 | isp.push('run foo') | |
|
233 | self.assertFalse(isp.push_accepts_more()) | |
|
199 | 234 | |
|
200 | 235 | def check_split(self, block_lines, compile=True): |
|
201 | 236 | blocks = assemble(block_lines) |
|
202 | 237 | lines = ''.join(blocks) |
|
203 |
oblock = self. |
|
|
238 | oblock = self.isp.split_blocks(lines) | |
|
204 | 239 | self.assertEqual(oblock, blocks) |
|
205 | 240 | if compile: |
|
206 | 241 | for block in blocks: |
|
207 |
self. |
|
|
242 | self.isp._compile(block) | |
|
208 | 243 | |
|
209 | 244 | def test_split(self): |
|
210 | 245 | # All blocks of input we want to test in a list. The format for each |
@@ -273,4 +308,39 b' class BlockBreakerTestCase(unittest.TestCase):' | |||
|
273 | 308 | ] |
|
274 | 309 | for block_lines in all_blocks: |
|
275 | 310 | self.check_split(block_lines, compile=False) |
|
311 | ||
|
312 | ||
|
313 | class InteractiveLoopTestCase(unittest.TestCase): | |
|
314 | """Tests for an interactive loop like a python shell. | |
|
315 | """ | |
|
316 | def check_ns(self, lines, ns): | |
|
317 | """Validate that the given input lines produce the resulting namespace. | |
|
318 | ||
|
319 | Note: the input lines are given exactly as they would be typed in an | |
|
320 | auto-indenting environment, as mini_interactive_loop above already does | |
|
321 | auto-indenting and prepends spaces to the input. | |
|
322 | """ | |
|
323 | src = mini_interactive_loop(pseudo_input(lines)) | |
|
324 | test_ns = {} | |
|
325 | exec src in test_ns | |
|
326 | # We can't check that the provided ns is identical to the test_ns, | |
|
327 | # because Python fills test_ns with extra keys (copyright, etc). But | |
|
328 | # we can check that the given dict is *contained* in test_ns | |
|
329 | for k,v in ns.items(): | |
|
330 | self.assertEqual(test_ns[k], v) | |
|
276 | 331 | |
|
332 | def test_simple(self): | |
|
333 | self.check_ns(['x=1'], dict(x=1)) | |
|
334 | ||
|
335 | def test_simple2(self): | |
|
336 | self.check_ns(['if 1:', 'x=2'], dict(x=2)) | |
|
337 | ||
|
338 | def test_xy(self): | |
|
339 | self.check_ns(['x=1; y=2'], dict(x=1, y=2)) | |
|
340 | ||
|
341 | def test_abc(self): | |
|
342 | self.check_ns(['if 1:','a=1','b=2','c=3'], dict(a=1, b=2, c=3)) | |
|
343 | ||
|
344 | def test_multi(self): | |
|
345 | self.check_ns(['x =(1+','1+','2)'], dict(x=4)) | |
|
346 |
General Comments 0
You need to be logged in to leave comments.
Login now