##// END OF EJS Templates
Simplify IPythonInputSplitter API
Thomas Kluyver -
Show More
@@ -536,14 +536,13 b' class IPythonInputSplitter(InputSplitter):'
536 self.transformer_accumulating = False
536 self.transformer_accumulating = False
537 self.within_python_line = False
537 self.within_python_line = False
538
538
539 last_exc = None
540 for t in self.transforms:
539 for t in self.transforms:
541 try:
540 try:
542 t.reset()
541 t.reset()
543 except SyntaxError as e:
542 except SyntaxError:
544 last_exc = e
543 # Nothing that calls reset() expects to handle transformer
545 if last_exc is not None:
544 # errors
546 raise last_exc
545 pass
547
546
548 def flush_transformers(self):
547 def flush_transformers(self):
549 def _flush(transform, out):
548 def _flush(transform, out):
@@ -560,18 +559,19 b' class IPythonInputSplitter(InputSplitter):'
560 if out is not None:
559 if out is not None:
561 self._store(out)
560 self._store(out)
562
561
563 def source_raw_reset(self):
562 def raw_reset(self):
564 """Return input and raw source and perform a full reset.
563 """Return raw input only and perform a full reset.
565 """
564 """
566 self.flush_transformers()
565 out = self.source_raw
567 out = self.source
568 out_r = self.source_raw
569 self.reset()
566 self.reset()
570 return out, out_r
567 return out
571
568
572 def source_reset(self):
569 def source_reset(self):
573 self.flush_transformers()
570 try:
574 return super(IPythonInputSplitter, self).source_reset()
571 self.flush_transformers()
572 return self.source
573 finally:
574 self.reset()
575
575
576 def push_accepts_more(self):
576 def push_accepts_more(self):
577 if self.transformer_accumulating:
577 if self.transformer_accumulating:
@@ -583,8 +583,12 b' class IPythonInputSplitter(InputSplitter):'
583 """Process and translate a cell of input.
583 """Process and translate a cell of input.
584 """
584 """
585 self.reset()
585 self.reset()
586 self.push(cell)
586 try:
587 return self.source_reset()
587 self.push(cell)
588 self.flush_transformers()
589 return self.source
590 finally:
591 self.reset()
588
592
589 def push(self, lines):
593 def push(self, lines):
590 """Push one or more lines of IPython input.
594 """Push one or more lines of IPython input.
@@ -2635,8 +2635,7 b' class InteractiveShell(SingletonConfigurable):'
2635 preprocessing_exc_tuple = None
2635 preprocessing_exc_tuple = None
2636 try:
2636 try:
2637 # Static input transformations
2637 # Static input transformations
2638 self.input_transformer_manager.push(raw_cell)
2638 cell = self.input_transformer_manager.transform_cell(raw_cell)
2639 cell = self.input_transformer_manager.source_reset()
2640 except SyntaxError:
2639 except SyntaxError:
2641 preprocessing_exc_tuple = sys.exc_info()
2640 preprocessing_exc_tuple = sys.exc_info()
2642 cell = raw_cell # cell has to exist so it can be stored/logged
2641 cell = raw_cell # cell has to exist so it can be stored/logged
@@ -412,7 +412,8 b' class IPythonInputTestCase(InputSplitterTestCase):'
412 continue
412 continue
413
413
414 isp.push(raw+'\n')
414 isp.push(raw+'\n')
415 out, out_raw = isp.source_raw_reset()
415 out_raw = isp.source_raw
416 out = isp.source_reset()
416 self.assertEqual(out.rstrip(), out_t,
417 self.assertEqual(out.rstrip(), out_t,
417 tt.pair_fail_msg.format("inputsplitter",raw, out_t, out))
418 tt.pair_fail_msg.format("inputsplitter",raw, out_t, out))
418 self.assertEqual(out_raw.rstrip(), raw.rstrip())
419 self.assertEqual(out_raw.rstrip(), raw.rstrip())
@@ -431,7 +432,8 b' class IPythonInputTestCase(InputSplitterTestCase):'
431 isp.push(lraw)
432 isp.push(lraw)
432 raw_parts.append(lraw)
433 raw_parts.append(lraw)
433
434
434 out, out_raw = isp.source_raw_reset()
435 out_raw = isp.source_raw
436 out = isp.source_reset()
435 out_t = '\n'.join(out_t_parts).rstrip()
437 out_t = '\n'.join(out_t_parts).rstrip()
436 raw = '\n'.join(raw_parts).rstrip()
438 raw = '\n'.join(raw_parts).rstrip()
437 self.assertEqual(out.rstrip(), out_t)
439 self.assertEqual(out.rstrip(), out_t)
@@ -498,7 +500,8 b" if __name__ == '__main__':"
498 # Here we just return input so we can use it in a test suite, but a
500 # Here we just return input so we can use it in a test suite, but a
499 # real interpreter would instead send it for execution somewhere.
501 # real interpreter would instead send it for execution somewhere.
500 #src = isp.source; raise EOFError # dbg
502 #src = isp.source; raise EOFError # dbg
501 src, raw = isp.source_raw_reset()
503 raw = isp.source_raw
504 src = isp.source_reset()
502 print('Input source was:\n', src)
505 print('Input source was:\n', src)
503 print('Raw source was:\n', raw)
506 print('Raw source was:\n', raw)
504 except EOFError:
507 except EOFError:
@@ -545,9 +548,7 b' class CellMagicsCommon(object):'
545
548
546 def test_whole_cell(self):
549 def test_whole_cell(self):
547 src = "%%cellm line\nbody\n"
550 src = "%%cellm line\nbody\n"
548 sp = self.sp
551 out = self.sp.transform_cell(src)
549 sp.push(src)
550 out = sp.source_reset()
551 ref = u"get_ipython().run_cell_magic({u}'cellm', {u}'line', {u}'body')\n"
552 ref = u"get_ipython().run_cell_magic({u}'cellm', {u}'line', {u}'body')\n"
552 nt.assert_equal(out, py3compat.u_format(ref))
553 nt.assert_equal(out, py3compat.u_format(ref))
553
554
@@ -204,10 +204,7 b' class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):'
204 prompt created. When triggered by an Enter/Return key press,
204 prompt created. When triggered by an Enter/Return key press,
205 'interactive' is True; otherwise, it is False.
205 'interactive' is True; otherwise, it is False.
206 """
206 """
207 try:
207 self._input_splitter.reset()
208 self._input_splitter.reset()
209 except SyntaxError:
210 pass
211 try:
208 try:
212 complete = self._input_splitter.push(source)
209 complete = self._input_splitter.push(source)
213 except SyntaxError:
210 except SyntaxError:
@@ -239,10 +236,7 b' class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):'
239 """
236 """
240 # Flush all state from the input splitter so the next round of
237 # Flush all state from the input splitter so the next round of
241 # reading input starts with a clean buffer.
238 # reading input starts with a clean buffer.
242 try:
239 self._input_splitter.reset()
243 self._input_splitter.reset()
244 except SyntaxError:
245 pass
246
240
247 if not self._reading:
241 if not self._reading:
248 self._highlighter.highlighting_on = False
242 self._highlighter.highlighting_on = False
@@ -258,7 +258,7 b' class EmbeddedSphinxShell(object):'
258 splitter.push(line)
258 splitter.push(line)
259 more = splitter.push_accepts_more()
259 more = splitter.push_accepts_more()
260 if not more:
260 if not more:
261 source_raw = splitter.source_raw_reset()[1]
261 source_raw = splitter.raw_reset()
262 self.IP.run_cell(source_raw, store_history=store_history)
262 self.IP.run_cell(source_raw, store_history=store_history)
263 finally:
263 finally:
264 sys.stdout = stdout
264 sys.stdout = stdout
@@ -455,7 +455,7 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
455 #double-guard against keyboardinterrupts during kbdint handling
455 #double-guard against keyboardinterrupts during kbdint handling
456 try:
456 try:
457 self.write('\nKeyboardInterrupt\n')
457 self.write('\nKeyboardInterrupt\n')
458 source_raw = self.input_splitter.source_raw_reset()[1]
458 source_raw = self.input_splitter.raw_reset()
459 hlen_b4_cell = self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
459 hlen_b4_cell = self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
460 more = False
460 more = False
461 except KeyboardInterrupt:
461 except KeyboardInterrupt:
@@ -483,7 +483,7 b' class ZMQTerminalInteractiveShell(TerminalInteractiveShell):'
483 self.autoedit_syntax):
483 self.autoedit_syntax):
484 self.edit_syntax_error()
484 self.edit_syntax_error()
485 if not more:
485 if not more:
486 source_raw = self.input_splitter.source_raw_reset()[1]
486 source_raw = self.input_splitter.raw_reset()
487 hlen_b4_cell = self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
487 hlen_b4_cell = self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
488 self.run_cell(source_raw)
488 self.run_cell(source_raw)
489
489
@@ -518,7 +518,7 b' class TerminalInteractiveShell(InteractiveShell):'
518 #double-guard against keyboardinterrupts during kbdint handling
518 #double-guard against keyboardinterrupts during kbdint handling
519 try:
519 try:
520 self.write('\nKeyboardInterrupt\n')
520 self.write('\nKeyboardInterrupt\n')
521 source_raw = self.input_splitter.source_raw_reset()[1]
521 source_raw = self.input_splitter.raw_reset()
522 hlen_b4_cell = \
522 hlen_b4_cell = \
523 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
523 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
524 more = False
524 more = False
@@ -552,7 +552,7 b' class TerminalInteractiveShell(InteractiveShell):'
552 self.autoedit_syntax):
552 self.autoedit_syntax):
553 self.edit_syntax_error()
553 self.edit_syntax_error()
554 if not more:
554 if not more:
555 source_raw = self.input_splitter.source_raw_reset()[1]
555 source_raw = self.input_splitter.raw_reset()
556 self.run_cell(source_raw, store_history=True)
556 self.run_cell(source_raw, store_history=True)
557 hlen_b4_cell = \
557 hlen_b4_cell = \
558 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
558 self._replace_rlhist_multiline(source_raw, hlen_b4_cell)
General Comments 0
You need to be logged in to leave comments. Login now