##// END OF EJS Templates
Renamed to inputsplitter, added more tests and examples....
Fernando Perez -
Show More
@@ -1,375 +1,414 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
13 12 #
14 13 # Distributed under the terms of the BSD License. The full license is in
15 14 # the file COPYING, distributed as part of this software.
16 15 #-----------------------------------------------------------------------------
17 16
18 17 #-----------------------------------------------------------------------------
19 18 # Imports
20 19 #-----------------------------------------------------------------------------
21 20 # stdlib
22 21 import codeop
23 22 import re
24 23 import sys
25 24
26 25 #-----------------------------------------------------------------------------
27 26 # Utilities
28 27 #-----------------------------------------------------------------------------
29 28
30 29 # FIXME: move these utilities to the general ward...
31 30
32 31 # compiled regexps for autoindent management
33 32 dedent_re = re.compile(r'^\s+raise|^\s+return|^\s+pass')
34 33 ini_spaces_re = re.compile(r'^([ \t\r\f\v]+)')
35 34
36 35
37 36 def num_ini_spaces(s):
38 37 """Return the number of initial spaces in a string.
39 38
40 39 Note that tabs are counted as a single space. For now, we do *not* support
41 40 mixing of tabs and spaces in the user's input.
42 41
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)
49 52 if ini_spaces:
50 53 return ini_spaces.end()
51 54 else:
52 55 return 0
53 56
54 57
55 58 def remove_comments(src):
56 59 """Remove all comments from input source.
57 60
58 61 Note: comments are NOT recognized inside of strings!
59 62
60 63 Parameters
61 64 ----------
62 65 src : string
63 66 A single or multiline input string.
64 67
65 68 Returns
66 69 -------
67 70 String with all Python comments removed.
68 71 """
69 72
70 73 return re.sub('#.*', '', src)
71 74
72 75
73 76 def get_input_encoding():
74 77 """Return the default standard input encoding."""
75 78 return getattr(sys.stdin, 'encoding', 'ascii')
76 79
77 80 #-----------------------------------------------------------------------------
78 81 # Classes and functions
79 82 #-----------------------------------------------------------------------------
80 83
81 class BlockBreaker(object):
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 BlockBreaker instance.
141 """Create a new InputSplitter instance.
106 142
107 143 Parameters
108 144 ----------
109 145 input_mode : str
110 146
111 147 One of 'append', 'replace', default is 'append'. This controls how
112 148 new inputs are used: in 'append' mode, they are appended to the
113 149 existing buffer and the whole buffer is compiled; in 'replace' mode,
114 150 each new input completely replaces all prior inputs. Replace mode is
115 151 thus equivalent to prepending a full reset() to every push() call.
116 152
117 153 In practice, line-oriented clients likely want to use 'append' mode
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 = BlockBreaker.input_mode if input_mode is None \
159 self.input_mode = InputSplitter.input_mode if input_mode is None \
124 160 else input_mode
125 161
126 162 def reset(self):
127 163 """Reset the input buffer and associated state."""
128 164 self.indent_spaces = 0
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.
137 173 """
138 174 out = self.source
139 175 self.reset()
140 176 return out
141 177
142 178 def push(self, lines):
143 179 """Push one ore more lines of input.
144 180
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 to propagate.
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 ----------
152 189 lines : string
153 190 One or more lines of Python input.
154 191
155 192 Returns
156 193 -------
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 an attribute so it can be queried at any
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()
165 202
166 203 # If the source code has leading blanks, add 'if 1:\n' to it
167 204 # this allows execution of indented pasted code. It is tempting
168 205 # to add '\n' at the end of source to run commands like ' a=1'
169 206 # directly, but this fails for more complicated scenarios
170 207 if not self._buffer and lines[:1] in [' ', '\t']:
171 208 lines = 'if 1:\n%s' % lines
172 209
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
187 224 # sent to the kernel for evaluation with possible ipython
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 interactive_block_ready(self):
200 """Return whether a block of interactive input is ready for execution.
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 BlockBreaker considers it has a complete
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
209 247 2. The indentation level is flush-left (because if we are indented,
210 248 like inside a function definition or for loop, we need to keep
211 249 reading new input).
212 250
213 251 3. There is one extra line consisting only of whitespace.
214 252
215 253 Because of condition #3, this method should be used only by
216 254 *line-oriented* frontends, since it means that intermediate blank lines
217 255 are not allowed in function definitions (or any other indented block).
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
239 279 Note: this method starts by performing a full reset().
240 280
241 281 Parameters
242 282 ----------
243 283 lines : str
244 284 A possibly multiline string.
245 285
246 286 Returns
247 287 -------
248 288 blocks : list
249 289 A list of strings, each possibly multiline. Each string corresponds
250 290 to a single block that can be compiled in 'single' mode (unless it
251 291 has a syntax error)."""
252 292
253 293 # This code is fairly delicate. If you make any changes here, make
254 294 # absolutely sure that you do run the full test suite and ALL tests
255 295 # pass.
256 296
257 297 self.reset()
258 298 blocks = []
259 299
260 300 # Reversed copy so we can use pop() efficiently and consume the input
261 301 # as a stack
262 302 lines = lines.splitlines()[::-1]
263 303 # Outer loop over all input
264 304 while lines:
265 305 # Inner loop to build each block
266 306 while True:
267 307 # Safety exit from inner loop
268 308 if not lines:
269 309 break
270 310 # Grab next line but don't push it yet
271 311 next_line = lines.pop()
272 312 # Blank/empty lines are pushed as-is
273 313 if not next_line or next_line.isspace():
274 314 self.push(next_line)
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
282 322 # return/raise/pass statement. These MUST be handled
283 323 # separately:
284 324 #
285 325 # 1. the first case is only detected when the actual explicit
286 326 # dedent happens, and that would be the *first* line of a *new*
287 327 # block. Thus, we must put the line back into the input buffer
288 328 # so that it starts a new block on the next pass.
289 329 #
290 330 # 2. the second case is detected in the line before the actual
291 331 # dedent happens, so , we consume the line and we can break out
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
299 339 # Otherwise any line is pushed
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.interactive_block_ready():
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())
308 348
309 349 return blocks
310 350
311 351 #------------------------------------------------------------------------
312 352 # Private interface
313 353 #------------------------------------------------------------------------
314 354
315 355 def _find_indent(self, line):
316 356 """Compute the new indentation level for a single line.
317 357
318 358 Parameters
319 359 ----------
320 360 line : str
321 361 A single new line of non-whitespace, non-comment Python input.
322 362
323 363 Returns
324 364 -------
325 365 indent_spaces : int
326 366 New value for the indent level (it may be equal to self.indent_spaces
327 367 if indentation doesn't change.
328 368
329 369 full_dedent : boolean
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:
337 377 indent_spaces = inisp
338 378 if indent_spaces <= 0:
339 379 #print 'Full dedent in text',self.source # dbg
340 380 full_dedent = True
341 381
342 382 if line[-1] == ':':
343 383 indent_spaces += 4
344 384 elif dedent_re.match(line):
345 385 indent_spaces -= 4
346 386 if indent_spaces <= 0:
347 387 full_dedent = True
348 388
349 389 # Safety
350 390 if indent_spaces < 0:
351 391 indent_spaces = 0
352 392 #print 'safety' # dbg
353 393
354 394 return indent_spaces, full_dedent
355 395
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.
363 403
364 404 If input lines are not newline-terminated, a newline is automatically
365 405 appended."""
366 406
367 407 if lines.endswith('\n'):
368 408 self._buffer.append(lines)
369 409 else:
370 410 self._buffer.append(lines+'\n')
371 411 self._set_source()
372 412
373 413 def _set_source(self):
374 414 self.source = ''.join(self._buffer).encode(self.encoding)
375
@@ -1,276 +1,346 b''
1 """Tests for the blockbreaker module.
1 """Tests for the inputsplitter module.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (C) 2010 The IPython Development Team
5 5 #
6 6 # Distributed under the terms of the BSD License. The full license is in
7 7 # the file COPYING, distributed as part of this software.
8 8 #-----------------------------------------------------------------------------
9 9
10 10 #-----------------------------------------------------------------------------
11 11 # Imports
12 12 #-----------------------------------------------------------------------------
13 13 # stdlib
14 14 import unittest
15 15
16 16 # Third party
17 17 import nose.tools as nt
18 18
19 19 # Our own
20 from IPython.core import blockbreaker as BB
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
24 51 #-----------------------------------------------------------------------------
25 52
26 53 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 #-----------------------------------------------------------------------------
33 71 def test_spaces():
34 72 tests = [('', 0),
35 73 (' ', 1),
36 74 ('\n', 0),
37 75 (' \n', 1),
38 76 ('x', 0),
39 77 (' x', 1),
40 78 (' x',2),
41 79 (' x',4),
42 80 # Note: tabs are counted as a single whitespace!
43 81 ('\tx', 1),
44 82 ('\t x', 2),
45 83 ]
46 84
47 85 for s, nsp in tests:
48 nt.assert_equal(BB.num_ini_spaces(s), nsp)
86 nt.assert_equal(isp.num_ini_spaces(s), nsp)
49 87
50 88
51 89 def test_remove_comments():
52 90 tests = [('text', 'text'),
53 91 ('text # comment', 'text '),
54 92 ('text # comment\n', 'text \n'),
55 93 ('text # comment \n', 'text \n'),
56 94 ('line # c \nline\n','line \nline\n'),
57 95 ('line # c \nline#c2 \nline\nline #c\n\n',
58 96 'line \nline\nline\nline \n\n'),
59 97 ]
60 98
61 99 for inp, out in tests:
62 nt.assert_equal(BB.remove_comments(inp), out)
100 nt.assert_equal(isp.remove_comments(inp), out)
63 101
64 102
65 103 def test_get_input_encoding():
66 encoding = BB.get_input_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 BlockBreakerTestCase(unittest.TestCase):
111 class InputSplitterTestCase(unittest.TestCase):
74 112 def setUp(self):
75 self.bb = BB.BlockBreaker()
113 self.isp = isp.InputSplitter()
76 114
77 115 def test_reset(self):
78 bb = self.bb
79 bb.push('x=1')
80 bb.reset()
81 self.assertEqual(bb._buffer, [])
82 self.assertEqual(bb.indent_spaces, 0)
83 self.assertEqual(bb.source, '')
84 self.assertEqual(bb.code, None)
85 self.assertEqual(bb.is_complete, False)
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.bb._store('1')
89 self.bb._store('2')
90 self.assertEqual(self.bb.source, '1\n2\n')
91 self.assertTrue(len(self.bb._buffer)>0)
92 self.assertEqual(self.bb.source_reset(), '1\n2\n')
93 self.assertEqual(self.bb._buffer, [])
94 self.assertEqual(self.bb.source, '')
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 bb = self.bb # shorthand
98 bb.push('x=1')
99 self.assertEqual(bb.indent_spaces, 0)
100 bb.push('if 1:\n x=1')
101 self.assertEqual(bb.indent_spaces, 4)
102 bb.push('y=2\n')
103 self.assertEqual(bb.indent_spaces, 0)
104 bb.push('if 1:')
105 self.assertEqual(bb.indent_spaces, 4)
106 bb.push(' x=1')
107 self.assertEqual(bb.indent_spaces, 4)
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 bb.push(' '*2)
110 self.assertEqual(bb.indent_spaces, 4)
147 isp.push(' '*2)
148 self.assertEqual(isp.indent_spaces, 4)
111 149
112 150 def test_indent2(self):
113 bb = self.bb
151 isp = self.isp
114 152 # When a multiline statement contains parens or multiline strings, we
115 153 # shouldn't get confused.
116 bb.push("if 1:")
117 bb.push(" x = (1+\n 2)")
118 self.assertEqual(bb.indent_spaces, 4)
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 bb = self.bb # shorthand
122 bb.push('if 1:')
123 self.assertEqual(bb.indent_spaces, 4)
124 bb.push(' pass')
125 self.assertEqual(bb.indent_spaces, 0)
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 bb = self.bb
129 bb.push('x=1')
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 bb = self.bb
134 bb.push('if 1:')
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 bb.push(line)
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 bb = self.bb
143 bb.push(' x=1')
144 bb.push(' y=2')
145 self.assertEqual(bb.source, 'if 1:\n x=1\n y=2\n')
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 bb = self.bb
149 bb.input_mode = 'replace'
150 bb.push('x=1')
151 self.assertEqual(bb.source, 'x=1\n')
152 bb.push('x=2')
153 self.assertEqual(bb.source, 'x=2\n')
154
155 def test_interactive_block_ready(self):
156 bb = self.bb
157 bb.push('x=1')
158 self.assertTrue(bb.interactive_block_ready())
159
160 def test_interactive_block_ready2(self):
161 bb = self.bb
162 bb.push('if 1:')
163 self.assertFalse(bb.interactive_block_ready())
164 bb.push(' x=1')
165 self.assertFalse(bb.interactive_block_ready())
166 bb.push('')
167 self.assertTrue(bb.interactive_block_ready())
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_interactive_block_ready3(self):
170 bb = self.bb
171 bb.push("x = (2+\n3)")
172 self.assertTrue(bb.interactive_block_ready())
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_interactive_block_ready4(self):
175 bb = self.bb
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
179 214 # multiline strings and multiline expressions (continued with \ or
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 bb.push("if 1:")
184 bb.push(" x = (2+")
185 bb.push(" 3)")
186 self.assertFalse(bb.interactive_block_ready())
187 bb.push(" y = 3")
188 self.assertFalse(bb.interactive_block_ready())
189 bb.push('')
190 self.assertTrue(bb.interactive_block_ready())
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 bb = self.bb
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 bb.push('run foo')
198 self.assertTrue(bb.interactive_block_ready())
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.bb.split_blocks(lines)
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.bb.compile(block)
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
211 246 # block is a list of lists, with each inner lists consisting of all the
212 247 # lines (as single-lines) that should make up a sub-block.
213 248
214 249 # Note: do NOT put here sub-blocks that don't compile, as the
215 250 # check_split() routine makes a final verification pass to check that
216 251 # each sub_block, as returned by split_blocks(), does compile
217 252 # correctly.
218 253 all_blocks = [ [['x=1']],
219 254
220 255 [['x=1'],
221 256 ['y=2']],
222 257
223 258 [['x=1'],
224 259 ['# a comment'],
225 260 ['y=11']],
226 261
227 262 [['if 1:',
228 263 ' x=1'],
229 264 ['y=3']],
230 265
231 266 [['def f(x):',
232 267 ' return x'],
233 268 ['x=1']],
234 269
235 270 [['def f(x):',
236 271 ' x+=1',
237 272 ' ',
238 273 ' return x'],
239 274 ['x=1']],
240 275
241 276 [['def f(x):',
242 277 ' if x>0:',
243 278 ' y=1',
244 279 ' # a comment',
245 280 ' else:',
246 281 ' y=4',
247 282 ' ',
248 283 ' return y'],
249 284 ['x=1'],
250 285 ['if 1:',
251 286 ' y=11'] ],
252 287
253 288 [['for i in range(10):'
254 289 ' x=i**2']],
255 290
256 291 [['for i in range(10):'
257 292 ' x=i**2'],
258 293 ['z = 1']],
259 294 ]
260 295 for block_lines in all_blocks:
261 296 self.check_split(block_lines)
262 297
263 298 def test_split_syntax_errors(self):
264 299 # Block splitting with invalid syntax
265 300 all_blocks = [ [['a syntax error']],
266 301
267 302 [['x=1'],
268 303 ['a syntax error']],
269 304
270 305 [['for i in range(10):'
271 306 ' an error']],
272 307
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