##// END OF EJS Templates
Add a few more fixes to cell/line input code, switch approaches....
Fernando Perez -
Show More
@@ -141,8 +141,6 b' def num_ini_spaces(s):'
141 else:
141 else:
142 return 0
142 return 0
143
143
144 last_blank_re = re.compile(r'^.*\n\s+$', re.MULTILINE)
145
146 def last_blank(src):
144 def last_blank(src):
147 """Determine if the input source ends in a blank.
145 """Determine if the input source ends in a blank.
148
146
@@ -153,11 +151,13 b' def last_blank(src):'
153 src : string
151 src : string
154 A single or multiline string.
152 A single or multiline string.
155 """
153 """
156 return src == '\n' or bool(last_blank_re.match(src))
154 if not src: return False
155 ll = src.splitlines()[-1]
156 return (ll == '') or ll.isspace()
157
157
158
158
159 last_two_blanks_re = re.compile(r'^\n\s*\n\s*$', re.MULTILINE)
159 last_two_blanks_re = re.compile(r'\n\s*\n\s*$', re.MULTILINE)
160 last_two_blanks_re2 = re.compile(r'^.+\n\s*\n\s+$', re.MULTILINE)
160 last_two_blanks_re2 = re.compile(r'.+\n\s*\n\s+$', re.MULTILINE)
161
161
162 def last_two_blanks(src):
162 def last_two_blanks(src):
163 """Determine if the input source ends in two blanks.
163 """Determine if the input source ends in two blanks.
@@ -169,8 +169,17 b' def last_two_blanks(src):'
169 src : string
169 src : string
170 A single or multiline string.
170 A single or multiline string.
171 """
171 """
172 return (bool(last_two_blanks_re.match(src)) or
172 if not src: return False
173 bool(last_two_blanks_re2.match(src)) )
173 # The logic here is tricky: I couldn't get a regexp to work and pass all
174 # the tests, so I took a different approach: split the source by lines,
175 # grab the last two and prepend '###\n' as a stand-in for whatever was in
176 # the body before the last two lines. Then, with that structure, it's
177 # possible to analyze with two regexps. Not the most elegant solution, but
178 # it works. If anyone tries to change this logic, make sure to validate
179 # the whole test suite first!
180 new_src = '\n'.join(['###\n'] + src.splitlines()[-2:])
181 return (bool(last_two_blanks_re.match(new_src)) or
182 bool(last_two_blanks_re2.match(new_src)) )
174
183
175
184
176 def remove_comments(src):
185 def remove_comments(src):
@@ -786,6 +795,7 b' class IPythonInputSplitter(InputSplitter):'
786 return False
795 return False
787
796
788 if self.cell_magic_mode:
797 if self.cell_magic_mode:
798 #print('c2 lines', repr(lines)) # dbg
789 # Find out if the last stored block has a whitespace line as its
799 # Find out if the last stored block has a whitespace line as its
790 # last line and also this line is whitespace, case in which we're
800 # last line and also this line is whitespace, case in which we're
791 # done (two contiguous blank lines signal termination). Note that
801 # done (two contiguous blank lines signal termination). Note that
@@ -842,7 +852,7 b' class IPythonInputSplitter(InputSplitter):'
842
852
843 This means that we get the entire cell with each call. Between resets,
853 This means that we get the entire cell with each call. Between resets,
844 the calls simply add more text to the input."""
854 the calls simply add more text to the input."""
845
855 print('lines', repr(lines)) # dbg
846 if lines.startswith('%%'):
856 if lines.startswith('%%'):
847 # Cell magics bypass all further transformations
857 # Cell magics bypass all further transformations
848 self.cell_magic_mode = True
858 self.cell_magic_mode = True
@@ -859,6 +869,7 b' class IPythonInputSplitter(InputSplitter):'
859 self._store(tlines)
869 self._store(tlines)
860 self._store(lines, self._buffer_raw, 'source_raw')
870 self._store(lines, self._buffer_raw, 'source_raw')
861 self._is_complete = last_two_blanks(lines)
871 self._is_complete = last_two_blanks(lines)
872 print('IC', self._is_complete) # dbg
862 return self._is_complete
873 return self._is_complete
863
874
864 lines_list = lines.splitlines()
875 lines_list = lines.splitlines()
@@ -609,6 +609,9 b' def test_last_blank():'
609 nt.assert_true(isp.last_blank('\n '))
609 nt.assert_true(isp.last_blank('\n '))
610 nt.assert_true(isp.last_blank('abc\n '))
610 nt.assert_true(isp.last_blank('abc\n '))
611 nt.assert_true(isp.last_blank('abc\n\n'))
611 nt.assert_true(isp.last_blank('abc\n\n'))
612 nt.assert_true(isp.last_blank('abc\nd\n\n'))
613 nt.assert_true(isp.last_blank('abc\nd\ne\n\n'))
614 nt.assert_true(isp.last_blank('abc \n \n \n\n'))
612
615
613
616
614 def test_last_two_blanks():
617 def test_last_two_blanks():
@@ -627,6 +630,8 b' def test_last_two_blanks():'
627 nt.assert_true(isp.last_two_blanks('abc\n\n \n'))
630 nt.assert_true(isp.last_two_blanks('abc\n\n \n'))
628 nt.assert_true(isp.last_two_blanks('abc\n\n \n '))
631 nt.assert_true(isp.last_two_blanks('abc\n\n \n '))
629 nt.assert_true(isp.last_two_blanks('abc\n\n \n \n'))
632 nt.assert_true(isp.last_two_blanks('abc\n\n \n \n'))
633 nt.assert_true(isp.last_two_blanks('abc\nd\n\n\n'))
634 nt.assert_true(isp.last_two_blanks('abc\nd\ns\nds\n\n\n'))
630
635
631
636
632 def test_cell_magics_line_mode():
637 def test_cell_magics_line_mode():
General Comments 0
You need to be logged in to leave comments. Login now