##// END OF EJS Templates
Add support for append/replace mode after discussion with Evan....
Fernando Perez -
Show More
@@ -91,16 +91,35 b' class BlockBreaker(object):'
91 code = None
91 code = None
92 # Boolean indicating whether the current block is complete
92 # Boolean indicating whether the current block is complete
93 is_complete = None
93 is_complete = None
94
94 # Input mode
95 input_mode = 'append'
96
95 # Private attributes
97 # Private attributes
96
98
97 # List
99 # List
98 _buffer = None
100 _buffer = None
99
101
100 def __init__(self):
102 def __init__(self, input_mode=None):
103 """Create a new BlockBreaker instance.
104
105 Parameters
106 ----------
107 input_mode : str
108
109 One of 'append', 'replace', default is 'append'. This controls how
110 new inputs are used: in 'append' mode, they are appended to the
111 existing buffer and the whole buffer is compiled; in 'replace' mode,
112 each new input completely replaces all prior inputs. Replace mode is
113 thus equivalent to prepending a full reset() to every push() call.
114
115 In practice, line-oriented clients likely want to use 'append' mode
116 while block-oriented ones will want to use 'replace'.
117 """
101 self._buffer = []
118 self._buffer = []
102 self.compile = codeop.CommandCompiler()
119 self.compile = codeop.CommandCompiler()
103 self.encoding = get_input_encoding()
120 self.encoding = get_input_encoding()
121 self.input_mode = BlockBreaker.input_mode if input_mode is None \
122 else input_mode
104
123
105 def reset(self):
124 def reset(self):
106 """Reset the input buffer and associated state."""
125 """Reset the input buffer and associated state."""
@@ -143,6 +162,9 b' class BlockBreaker(object):'
143 this value is also stored as an attribute so it can be queried at any
162 this value is also stored as an attribute so it can be queried at any
144 time.
163 time.
145 """
164 """
165 if self.input_mode == 'replace':
166 self.reset()
167
146 # If the source code has leading blanks, add 'if 1:\n' to it
168 # If the source code has leading blanks, add 'if 1:\n' to it
147 # this allows execution of indented pasted code. It is tempting
169 # this allows execution of indented pasted code. It is tempting
148 # to add '\n' at the end of source to run commands like ' a=1'
170 # to add '\n' at the end of source to run commands like ' a=1'
@@ -137,6 +137,14 b' class BlockBreakerTestCase(unittest.TestCase):'
137 bb.push(' y=2')
137 bb.push(' y=2')
138 self.assertEqual(bb.source, 'if 1:\n x=1\n y=2\n')
138 self.assertEqual(bb.source, 'if 1:\n x=1\n y=2\n')
139
139
140 def test_replace_mode(self):
141 bb = self.bb
142 bb.input_mode = 'replace'
143 bb.push('x=1')
144 self.assertEqual(bb.source, 'x=1\n')
145 bb.push('x=2')
146 self.assertEqual(bb.source, 'x=2\n')
147
140 def test_interactive_block_ready(self):
148 def test_interactive_block_ready(self):
141 bb = self.bb
149 bb = self.bb
142 bb.push('x=1')
150 bb.push('x=1')
@@ -144,7 +152,9 b' class BlockBreakerTestCase(unittest.TestCase):'
144
152
145 def test_interactive_block_ready2(self):
153 def test_interactive_block_ready2(self):
146 bb = self.bb
154 bb = self.bb
147 bb.push('if 1:\n x=1')
155 bb.push('if 1:')
156 self.assertFalse(bb.interactive_block_ready())
157 bb.push(' x=1')
148 self.assertFalse(bb.interactive_block_ready())
158 self.assertFalse(bb.interactive_block_ready())
149 bb.push('')
159 bb.push('')
150 self.assertTrue(bb.interactive_block_ready())
160 self.assertTrue(bb.interactive_block_ready())
General Comments 0
You need to be logged in to leave comments. Login now