diff --git a/IPython/core/inputsplitter.py b/IPython/core/inputsplitter.py index eebbb7e..dc57840 100644 --- a/IPython/core/inputsplitter.py +++ b/IPython/core/inputsplitter.py @@ -545,9 +545,9 @@ class IPythonInputSplitter(InputSplitter): def transforms_in_use(self): """Transformers, excluding logical line transformers if we're in a Python line.""" - t = self.physical_line_transforms + [self.assemble_logical_lines] + t = self.physical_line_transforms[:] if not self.within_python_line: - t += self.logical_line_transforms + t += [self.assemble_logical_lines] + self.logical_line_transforms return t + [self.assemble_python_lines] + self.python_line_transforms def reset(self): @@ -556,6 +556,7 @@ class IPythonInputSplitter(InputSplitter): self._buffer_raw[:] = [] self.source_raw = '' self.transformer_accumulating = False + self.within_python_line = False for t in self.transforms: t.reset() @@ -685,11 +686,11 @@ class IPythonInputSplitter(InputSplitter): if line is None: return _accumulating(transformer) - line = self.assemble_logical_lines.push(line) - if line is None: - return _accumulating('acc logical line') - if not self.within_python_line: + line = self.assemble_logical_lines.push(line) + if line is None: + return _accumulating('acc logical line') + for transformer in self.logical_line_transforms: line = transformer.push(line) if line is None: diff --git a/IPython/core/inputtransformer.py b/IPython/core/inputtransformer.py index f5a9fe5..4606623 100644 --- a/IPython/core/inputtransformer.py +++ b/IPython/core/inputtransformer.py @@ -180,13 +180,15 @@ def assemble_logical_lines(): parts = [] while line is not None: - parts.append(line.rstrip('\\')) - if not line.endswith('\\'): + if line.endswith('\\') and (not has_comment(line)): + parts.append(line[:-1]) + line = (yield None) # Get another line + else: + parts.append(line) break - line = (yield None) # Output - line = ' '.join(parts) + line = ''.join(parts) # Utilities def _make_help_call(target, esc, lspace, next_input=None): diff --git a/IPython/core/tests/test_inputtransformer.py b/IPython/core/tests/test_inputtransformer.py index 48f15bf..2fddf97 100644 --- a/IPython/core/tests/test_inputtransformer.py +++ b/IPython/core/tests/test_inputtransformer.py @@ -190,11 +190,18 @@ syntax_ml = \ ], ], - multiline_datastructure = + multiline_datastructure_prompt = [ [('>>> a = [1,','a = [1,'), ('... 2]','2]'), ], ], + + multiline_datastructure = + [ [('b = ("%s"', None), + ('# comment', None), + ('%foo )', 'b = ("%s"\n# comment\n%foo )'), + ], + ], leading_indent = [ [(' print "hi"','print "hi"'), @@ -222,31 +229,31 @@ syntax_ml = \ escaped = [ [('%abc def \\', None), - ('ghi', u_fmt("get_ipython().magic({u}'abc def ghi')")), + ('ghi', u_fmt("get_ipython().magic({u}'abc def ghi')")), ], [('%abc def \\', None), ('ghi\\', None), - (None, u_fmt("get_ipython().magic({u}'abc def ghi')")), + (None, u_fmt("get_ipython().magic({u}'abc def ghi')")), ], ], assign_magic = [ [(u'a = %bc de \\', None), - (u'fg', u_fmt("a = get_ipython().magic({u}'bc de fg')")), + (u'fg', u_fmt("a = get_ipython().magic({u}'bc de fg')")), ], [(u'a = %bc de \\', None), (u'fg\\', None), - (None, u_fmt("a = get_ipython().magic({u}'bc de fg')")), + (None, u_fmt("a = get_ipython().magic({u}'bc de fg')")), ], ], assign_system = [ [(u'a = !bc de \\', None), - (u'fg', u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), + (u'fg', u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), ], [(u'a = !bc de \\', None), (u'fg\\', None), - (None, u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), + (None, u_fmt("a = get_ipython().getoutput({u}'bc de fg')")), ], ], ) @@ -262,7 +269,7 @@ def test_classic_prompt(): tt.check_pairs(transform_and_reset(ipt.classic_prompt), syntax['classic_prompt']) for example in syntax_ml['classic_prompt']: transform_checker(example, ipt.classic_prompt) - for example in syntax_ml['multiline_datastructure']: + for example in syntax_ml['multiline_datastructure_prompt']: transform_checker(example, ipt.classic_prompt) @@ -274,11 +281,13 @@ def test_ipy_prompt(): def test_assemble_logical_lines(): tests = \ [ [(u"a = \\", None), - (u"123", u"a = 123"), + (u"123", u"a = 123"), ], [(u"a = \\", None), # Test resetting when within a multi-line string (u"12 *\\", None), - (None, u"a = 12 *"), + (None, u"a = 12 *"), + ], + [(u"# foo\\", u"# foo\\"), # Comments can't be continued like this ], ] for example in tests: @@ -300,7 +309,7 @@ def test_assemble_python_lines(): (u"2,", None), (None, u"a = [1,\n2,"), ], - ] + ] + syntax_ml['multiline_datastructure'] for example in tests: transform_checker(example, ipt.assemble_python_lines)