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