##// END OF EJS Templates
Renamed to inputsplitter, added more tests and examples....
Fernando Perez -
Show More
@@ -1,12 +1,11 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
@@ -43,6 +42,10 b' def num_ini_spaces(s):'
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)
@@ -78,31 +81,64 b' def get_input_encoding():'
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 ----------
@@ -118,9 +154,9 b' class BlockBreaker(object):'
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):
@@ -129,8 +165,8 b' class BlockBreaker(object):'
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.
@@ -145,7 +181,8 b' class BlockBreaker(object):'
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 ----------
@@ -157,8 +194,8 b' class BlockBreaker(object):'
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()
@@ -173,14 +210,14 b' class BlockBreaker(object):'
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
@@ -188,21 +225,22 b' class BlockBreaker(object):'
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
@@ -218,21 +256,23 b' class BlockBreaker(object):'
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
@@ -275,7 +315,7 b' class BlockBreaker(object):'
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
@@ -292,7 +332,7 b' class BlockBreaker(object):'
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
@@ -300,8 +340,8 b' class BlockBreaker(object):'
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())
@@ -330,7 +370,7 b' class BlockBreaker(object):'
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:
@@ -356,7 +396,7 b' class BlockBreaker(object):'
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.
@@ -372,4 +412,3 b' class BlockBreaker(object):'
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,4 +1,4 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
@@ -17,7 +17,34 b' import unittest'
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
@@ -27,6 +54,17 b' 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 #-----------------------------------------------------------------------------
@@ -45,7 +83,7 b' def test_spaces():'
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():
@@ -59,120 +97,117 b' def test_remove_comments():'
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
@@ -180,31 +215,31 b' class BlockBreakerTestCase(unittest.TestCase):'
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
@@ -273,4 +308,39 b' class BlockBreakerTestCase(unittest.TestCase):'
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