From 745a5be258dc5be7f4c424ea566760d1ec69f499 2013-12-19 20:03:47 From: Thomas Kluyver Date: 2013-12-19 20:03:47 Subject: [PATCH] Simplify IPythonInputSplitter API --- diff --git a/IPython/core/inputsplitter.py b/IPython/core/inputsplitter.py index 7c4e75e..6885490 100644 --- a/IPython/core/inputsplitter.py +++ b/IPython/core/inputsplitter.py @@ -536,14 +536,13 @@ class IPythonInputSplitter(InputSplitter): self.transformer_accumulating = False self.within_python_line = False - last_exc = None for t in self.transforms: try: t.reset() - except SyntaxError as e: - last_exc = e - if last_exc is not None: - raise last_exc + except SyntaxError: + # Nothing that calls reset() expects to handle transformer + # errors + pass def flush_transformers(self): def _flush(transform, out): @@ -560,18 +559,19 @@ class IPythonInputSplitter(InputSplitter): if out is not None: self._store(out) - def source_raw_reset(self): - """Return input and raw source and perform a full reset. + def raw_reset(self): + """Return raw input only and perform a full reset. """ - self.flush_transformers() - out = self.source - out_r = self.source_raw + out = self.source_raw self.reset() - return out, out_r + return out def source_reset(self): - self.flush_transformers() - return super(IPythonInputSplitter, self).source_reset() + try: + self.flush_transformers() + return self.source + finally: + self.reset() def push_accepts_more(self): if self.transformer_accumulating: @@ -583,8 +583,12 @@ class IPythonInputSplitter(InputSplitter): """Process and translate a cell of input. """ self.reset() - self.push(cell) - return self.source_reset() + try: + self.push(cell) + self.flush_transformers() + return self.source + finally: + self.reset() def push(self, lines): """Push one or more lines of IPython input. diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 7067557..5453ade 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2635,8 +2635,7 @@ class InteractiveShell(SingletonConfigurable): preprocessing_exc_tuple = None try: # Static input transformations - self.input_transformer_manager.push(raw_cell) - cell = self.input_transformer_manager.source_reset() + cell = self.input_transformer_manager.transform_cell(raw_cell) except SyntaxError: preprocessing_exc_tuple = sys.exc_info() cell = raw_cell # cell has to exist so it can be stored/logged diff --git a/IPython/core/tests/test_inputsplitter.py b/IPython/core/tests/test_inputsplitter.py index e579006..79761fe 100644 --- a/IPython/core/tests/test_inputsplitter.py +++ b/IPython/core/tests/test_inputsplitter.py @@ -412,7 +412,8 @@ class IPythonInputTestCase(InputSplitterTestCase): continue isp.push(raw+'\n') - out, out_raw = isp.source_raw_reset() + out_raw = isp.source_raw + out = isp.source_reset() self.assertEqual(out.rstrip(), out_t, tt.pair_fail_msg.format("inputsplitter",raw, out_t, out)) self.assertEqual(out_raw.rstrip(), raw.rstrip()) @@ -431,7 +432,8 @@ class IPythonInputTestCase(InputSplitterTestCase): isp.push(lraw) raw_parts.append(lraw) - out, out_raw = isp.source_raw_reset() + out_raw = isp.source_raw + out = isp.source_reset() out_t = '\n'.join(out_t_parts).rstrip() raw = '\n'.join(raw_parts).rstrip() self.assertEqual(out.rstrip(), out_t) @@ -498,7 +500,8 @@ if __name__ == '__main__': # Here we just return input so we can use it in a test suite, but a # real interpreter would instead send it for execution somewhere. #src = isp.source; raise EOFError # dbg - src, raw = isp.source_raw_reset() + raw = isp.source_raw + src = isp.source_reset() print('Input source was:\n', src) print('Raw source was:\n', raw) except EOFError: @@ -545,9 +548,7 @@ class CellMagicsCommon(object): def test_whole_cell(self): src = "%%cellm line\nbody\n" - sp = self.sp - sp.push(src) - out = sp.source_reset() + out = self.sp.transform_cell(src) ref = u"get_ipython().run_cell_magic({u}'cellm', {u}'line', {u}'body')\n" nt.assert_equal(out, py3compat.u_format(ref)) diff --git a/IPython/qt/console/frontend_widget.py b/IPython/qt/console/frontend_widget.py index 79425b0..0a7e7d5 100644 --- a/IPython/qt/console/frontend_widget.py +++ b/IPython/qt/console/frontend_widget.py @@ -204,10 +204,7 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): prompt created. When triggered by an Enter/Return key press, 'interactive' is True; otherwise, it is False. """ - try: - self._input_splitter.reset() - except SyntaxError: - pass + self._input_splitter.reset() try: complete = self._input_splitter.push(source) except SyntaxError: @@ -239,10 +236,7 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): """ # Flush all state from the input splitter so the next round of # reading input starts with a clean buffer. - try: - self._input_splitter.reset() - except SyntaxError: - pass + self._input_splitter.reset() if not self._reading: self._highlighter.highlighting_on = False diff --git a/IPython/sphinxext/ipython_directive.py b/IPython/sphinxext/ipython_directive.py index 17c3c65..b20d256 100644 --- a/IPython/sphinxext/ipython_directive.py +++ b/IPython/sphinxext/ipython_directive.py @@ -258,7 +258,7 @@ class EmbeddedSphinxShell(object): splitter.push(line) more = splitter.push_accepts_more() if not more: - source_raw = splitter.source_raw_reset()[1] + source_raw = splitter.raw_reset() self.IP.run_cell(source_raw, store_history=store_history) finally: sys.stdout = stdout diff --git a/IPython/terminal/console/interactiveshell.py b/IPython/terminal/console/interactiveshell.py index 902c6d5..13f95d0 100644 --- a/IPython/terminal/console/interactiveshell.py +++ b/IPython/terminal/console/interactiveshell.py @@ -455,7 +455,7 @@ class ZMQTerminalInteractiveShell(TerminalInteractiveShell): #double-guard against keyboardinterrupts during kbdint handling try: self.write('\nKeyboardInterrupt\n') - source_raw = self.input_splitter.source_raw_reset()[1] + source_raw = self.input_splitter.raw_reset() hlen_b4_cell = self._replace_rlhist_multiline(source_raw, hlen_b4_cell) more = False except KeyboardInterrupt: @@ -483,7 +483,7 @@ class ZMQTerminalInteractiveShell(TerminalInteractiveShell): self.autoedit_syntax): self.edit_syntax_error() if not more: - source_raw = self.input_splitter.source_raw_reset()[1] + source_raw = self.input_splitter.raw_reset() hlen_b4_cell = self._replace_rlhist_multiline(source_raw, hlen_b4_cell) self.run_cell(source_raw) diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py index 434e134..c041cb1 100644 --- a/IPython/terminal/interactiveshell.py +++ b/IPython/terminal/interactiveshell.py @@ -518,7 +518,7 @@ class TerminalInteractiveShell(InteractiveShell): #double-guard against keyboardinterrupts during kbdint handling try: self.write('\nKeyboardInterrupt\n') - source_raw = self.input_splitter.source_raw_reset()[1] + source_raw = self.input_splitter.raw_reset() hlen_b4_cell = \ self._replace_rlhist_multiline(source_raw, hlen_b4_cell) more = False @@ -552,7 +552,7 @@ class TerminalInteractiveShell(InteractiveShell): self.autoedit_syntax): self.edit_syntax_error() if not more: - source_raw = self.input_splitter.source_raw_reset()[1] + source_raw = self.input_splitter.raw_reset() self.run_cell(source_raw, store_history=True) hlen_b4_cell = \ self._replace_rlhist_multiline(source_raw, hlen_b4_cell)