From 7462a578a362e91323e8255d3f89866551e54a09 2011-05-27 17:58:10 From: MinRK Date: 2011-05-27 17:58:10 Subject: [PATCH] fix SyntaxError on !(command) prefilter_line expects that trailing newlines will be trimmed, but run_cell passed it lines with a trailing \n. Single-line run_cell now uses prefilter_lines again, and appends newline explicitly. Formerly failing test included. --- diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 2795b52..d4988e7 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -2172,7 +2172,9 @@ class InteractiveShell(SingletonConfigurable, Magic): prefilter_failed = False if len(cell.splitlines()) == 1: try: - cell = self.prefilter_manager.prefilter_line(cell) + # use prefilter_lines to handle trailing newlines + # restore trailing newline for ast.parse + cell = self.prefilter_manager.prefilter_lines(cell) + '\n' except AliasError as e: error(e) prefilter_failed=True diff --git a/IPython/core/tests/test_interactiveshell.py b/IPython/core/tests/test_interactiveshell.py index 904e6f8..b4e5fcf 100644 --- a/IPython/core/tests/test_interactiveshell.py +++ b/IPython/core/tests/test_interactiveshell.py @@ -106,4 +106,11 @@ class InteractiveShellTestCase(unittest.TestCase): err = io.stderr.getvalue() io.stderr = save_err self.assertEquals(err.split(':')[0], 'ERROR') + + def test_trailing_newline(self): + """test that running !(command) does not raise a SyntaxError""" + ip = get_ipython() + ip.run_cell('!(true)\n', False) + ip.run_cell('!(true)\n\n\n', False) +