diff --git a/IPython/core/inputtransformer2.py b/IPython/core/inputtransformer2.py index 29044d6..0027013 100644 --- a/IPython/core/inputtransformer2.py +++ b/IPython/core/inputtransformer2.py @@ -61,9 +61,10 @@ def cell_magic(lines): if re.match('%%\w+\?', lines[0]): # This case will be handled by help_end return lines - magic_name, first_line = lines[0][2:].partition(' ') - body = '\n'.join(lines[1:]) - return ['get_ipython().run_cell_magic(%r, %r, %r)' % (magic_name, first_line, body)] + magic_name, _, first_line = lines[0][2:-1].partition(' ') + body = ''.join(lines[1:]) + return ['get_ipython().run_cell_magic(%r, %r, %r)\n' + % (magic_name, first_line, body)] line_transforms = [ leading_indent, @@ -430,5 +431,4 @@ def transform_cell(cell): lines = transform(lines) lines = TokenTransformers()(lines) - for line in lines: - print('~~', line) + return ''.join(lines) diff --git a/IPython/core/tests/test_inputtransformer2_line.py b/IPython/core/tests/test_inputtransformer2_line.py new file mode 100644 index 0000000..9139e58 --- /dev/null +++ b/IPython/core/tests/test_inputtransformer2_line.py @@ -0,0 +1,21 @@ +"""Tests for the line-based transformers in IPython.core.inputtransformer2 + +Line-based transformers are the simpler ones; token-based transformers are +more complex. +""" +import nose.tools as nt + +from IPython.core import inputtransformer2 as ipt2 + +SIMPLE = ("""\ +%%foo arg +body 1 +body 2 +""", """\ +get_ipython().run_cell_magic('foo', 'arg', 'body 1\\nbody 2\\n') +""") + +def test_cell_magic(): + for sample, expected in [SIMPLE]: + nt.assert_equal(ipt2.cell_magic(sample.splitlines(keepends=True)), + expected.splitlines(keepends=True)) diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 5225ec3..17f289c 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -700,8 +700,8 @@ class CellMagicTestCase(TestCase): out = _ip.run_cell_magic(magic, 'a', 'b') nt.assert_equal(out, ('a','b')) # Via run_cell, it goes into the user's namespace via displayhook - _ip.run_cell('%%' + magic +' c\nd') - nt.assert_equal(_ip.user_ns['_'], ('c','d')) + _ip.run_cell('%%' + magic +' c\nd\n') + nt.assert_equal(_ip.user_ns['_'], ('c','d\n')) def test_cell_magic_func_deco(self): "Cell magic using simple decorator"